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 struct fd *serial_get_fd( struct object *obj );
56 static void serial_destroy(struct object *obj);
58 static int serial_get_poll_events( struct fd *fd );
59 static void serial_poll_event( struct fd *fd, int event );
60 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
61 static int serial_flush( struct fd *fd, struct event **event );
62 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count);
72 unsigned int readinterval;
73 unsigned int readconst;
74 unsigned int readmult;
75 unsigned int writeconst;
76 unsigned int writemult;
78 unsigned int eventmask;
79 unsigned int commerror;
81 struct termios original;
83 struct async_queue read_q;
84 struct async_queue write_q;
85 struct async_queue wait_q;
87 /* FIXME: add dcb, comm status, handler module, sharing */
90 static const struct object_ops serial_ops =
92 sizeof(struct serial), /* size */
93 serial_dump, /* dump */
94 default_fd_add_queue, /* add_queue */
95 default_fd_remove_queue, /* remove_queue */
96 default_fd_signaled, /* signaled */
97 no_satisfied, /* satisfied */
98 serial_get_fd, /* get_fd */
99 serial_destroy /* destroy */
102 static const struct fd_ops serial_fd_ops =
104 serial_get_poll_events, /* get_poll_events */
105 serial_poll_event, /* poll_event */
106 serial_flush, /* flush */
107 serial_get_info, /* get_file_info */
108 serial_queue_async /* queue_async */
111 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
113 struct serial *serial;
118 if (!(name = mem_alloc( len + 1 ))) return NULL;
119 memcpy( name, nameptr, len );
122 switch(access & (GENERIC_READ | GENERIC_WRITE))
124 case GENERIC_READ: flags |= O_RDONLY; break;
125 case GENERIC_WRITE: flags |= O_WRONLY; break;
126 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
132 fd = open( name, flags );
140 /* check its really a serial port */
141 if (tcgetattr(fd,&tios))
148 /* set the fd back to blocking if necessary */
149 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
150 if(0>fcntl(fd, F_SETFL, 0))
153 if (!(serial = alloc_object( &serial_ops )))
158 serial->attrib = attributes;
159 serial->access = access;
160 serial->readinterval = 0;
161 serial->readmult = 0;
162 serial->readconst = 0;
163 serial->writemult = 0;
164 serial->writeconst = 0;
165 serial->eventmask = 0;
166 serial->commerror = 0;
167 init_async_queue(&serial->read_q);
168 init_async_queue(&serial->write_q);
169 init_async_queue(&serial->wait_q);
170 if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, fd, &serial->obj )))
172 release_object( serial );
178 static struct fd *serial_get_fd( struct object *obj )
180 struct serial *serial = (struct serial *)obj;
181 return (struct fd *)grab_object( serial->fd );
184 static void serial_destroy( struct object *obj)
186 struct serial *serial = (struct serial *)obj;
188 destroy_async_queue(&serial->read_q);
189 destroy_async_queue(&serial->write_q);
190 destroy_async_queue(&serial->wait_q);
191 if (serial->fd) release_object( serial->fd );
194 static void serial_dump( struct object *obj, int verbose )
196 struct serial *serial = (struct serial *)obj;
197 assert( obj->ops == &serial_ops );
198 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
201 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
203 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
206 static int serial_get_poll_events( struct fd *fd )
208 struct serial *serial = get_fd_user( fd );
210 assert( serial->obj.ops == &serial_ops );
212 if(IS_READY(serial->read_q))
214 if(IS_READY(serial->write_q))
216 if(IS_READY(serial->wait_q))
219 /* fprintf(stderr,"poll events are %04x\n",events); */
224 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
226 struct serial *serial = get_fd_user( fd );
227 assert( serial->obj.ops == &serial_ops );
231 reply->type = FILE_TYPE_CHAR;
233 reply->access_time = 0;
234 reply->write_time = 0;
235 reply->size_high = 0;
238 reply->index_high = 0;
239 reply->index_low = 0;
244 if(serial->attrib & FILE_FLAG_OVERLAPPED)
245 *flags |= FD_FLAG_OVERLAPPED;
246 else if(!((serial->readinterval == MAXDWORD) &&
247 (serial->readmult == 0) && (serial->readconst == 0)) )
248 *flags |= FD_FLAG_TIMEOUT;
250 return FD_TYPE_DEFAULT;
253 static void serial_poll_event(struct fd *fd, int event)
255 struct serial *serial = get_fd_user( fd );
257 /* fprintf(stderr,"Poll event %02x\n",event); */
259 if(IS_READY(serial->read_q) && (POLLIN & event) )
260 async_notify(serial->read_q.head,STATUS_ALERTED);
262 if(IS_READY(serial->write_q) && (POLLOUT & event) )
263 async_notify(serial->write_q.head,STATUS_ALERTED);
265 if(IS_READY(serial->wait_q) && (POLLIN & event) )
266 async_notify(serial->wait_q.head,STATUS_ALERTED);
268 set_fd_events( fd, serial_get_poll_events(fd) );
271 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
273 struct serial *serial = get_fd_user( fd );
274 struct async_queue *q;
278 assert(serial->obj.ops == &serial_ops);
282 case ASYNC_TYPE_READ:
284 timeout = serial->readconst + serial->readmult*count;
286 case ASYNC_TYPE_WAIT:
290 case ASYNC_TYPE_WRITE:
291 q = &serial->write_q;
292 timeout = serial->writeconst + serial->writemult*count;
295 set_error(STATUS_INVALID_PARAMETER);
299 async = find_async ( q, current, ptr );
301 if ( status == STATUS_PENDING )
306 async = create_async ( &serial->obj, current, ptr );
310 async->status = STATUS_PENDING;
313 async_add_timeout(async,timeout);
314 async_insert(q, async);
317 /* Check if the new pending request can be served immediately */
318 events = check_fd_events( fd, serial_get_poll_events( fd ) );
321 /* serial_poll_event() calls set_select_events() */
322 serial_poll_event( fd, events );
326 else if ( async ) destroy_async ( async );
327 else set_error ( STATUS_INVALID_PARAMETER );
329 set_fd_events ( fd, serial_get_poll_events( fd ));
332 static int serial_flush( struct fd *fd, struct event **event )
334 /* MSDN says: If hFile is a handle to a communications device,
335 * the function only flushes the transmit buffer.
337 int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
338 if (!ret) file_set_error();
342 /* create a serial */
343 DECL_HANDLER(create_serial)
345 struct serial *serial;
348 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
350 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
351 release_object( serial );
355 DECL_HANDLER(get_serial_info)
357 struct serial *serial;
359 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
362 reply->readinterval = serial->readinterval;
363 reply->readconst = serial->readconst;
364 reply->readmult = serial->readmult;
365 reply->writeconst = serial->writeconst;
366 reply->writemult = serial->writemult;
369 reply->eventmask = serial->eventmask;
371 /* comm port error status */
372 reply->commerror = serial->commerror;
374 release_object( serial );
378 DECL_HANDLER(set_serial_info)
380 struct serial *serial;
382 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
385 if(req->flags & SERIALINFO_SET_TIMEOUTS)
387 serial->readinterval = req->readinterval;
388 serial->readconst = req->readconst;
389 serial->readmult = req->readmult;
390 serial->writeconst = req->writeconst;
391 serial->writemult = req->writemult;
395 if(req->flags & SERIALINFO_SET_MASK)
397 serial->eventmask = req->eventmask;
398 if(!serial->eventmask)
400 while(serial->wait_q.head)
402 async_notify(serial->wait_q.head, STATUS_SUCCESS);
403 destroy_async(serial->wait_q.head);
408 /* comm port error status */
409 if(req->flags & SERIALINFO_SET_ERROR)
411 serial->commerror = req->commerror;
414 release_object( serial );