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 );
66 unsigned int readinterval;
67 unsigned int readconst;
68 unsigned int readmult;
69 unsigned int writeconst;
70 unsigned int writemult;
72 unsigned int eventmask;
73 unsigned int commerror;
75 struct termios original;
77 struct async_queue read_q;
78 struct async_queue write_q;
79 struct async_queue wait_q;
81 /* FIXME: add dcb, comm status, handler module, sharing */
84 static const struct object_ops serial_ops =
86 sizeof(struct serial), /* size */
87 serial_dump, /* dump */
88 default_poll_add_queue, /* add_queue */
89 default_poll_remove_queue, /* remove_queue */
90 default_poll_signaled, /* signaled */
91 no_satisfied, /* satisfied */
92 serial_get_poll_events, /* get_poll_events */
93 serial_poll_event, /* poll_event */
94 serial_get_fd, /* get_fd */
96 serial_get_info, /* get_file_info */
97 serial_queue_async, /* queue_async */
98 destroy_serial /* destroy */
101 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
103 struct serial *serial;
108 if (!(name = mem_alloc( len + 1 ))) return NULL;
109 memcpy( name, nameptr, len );
112 switch(access & (GENERIC_READ | GENERIC_WRITE))
114 case GENERIC_READ: flags |= O_RDONLY; break;
115 case GENERIC_WRITE: flags |= O_WRONLY; break;
116 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
122 fd = open( name, flags );
130 /* check its really a serial port */
131 if (tcgetattr(fd,&tios))
138 /* set the fd back to blocking if necessary */
139 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
140 if(0>fcntl(fd, F_SETFL, 0))
143 if ((serial = alloc_object( &serial_ops, fd )))
145 serial->attrib = attributes;
146 serial->access = access;
147 serial->readinterval = 0;
148 serial->readmult = 0;
149 serial->readconst = 0;
150 serial->writemult = 0;
151 serial->writeconst = 0;
152 serial->eventmask = 0;
153 serial->commerror = 0;
154 init_async_queue(&serial->read_q);
155 init_async_queue(&serial->write_q);
156 init_async_queue(&serial->wait_q);
161 static void destroy_serial( struct object *obj)
163 struct serial *serial = (struct serial *)obj;
165 destroy_async_queue(&serial->read_q);
166 destroy_async_queue(&serial->write_q);
167 destroy_async_queue(&serial->wait_q);
170 static void serial_dump( struct object *obj, int verbose )
172 struct serial *serial = (struct serial *)obj;
173 assert( obj->ops == &serial_ops );
174 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
177 struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
179 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
182 static int serial_get_poll_events( struct object *obj )
184 struct serial *serial = (struct serial *)obj;
186 assert( obj->ops == &serial_ops );
188 if(IS_READY(serial->read_q))
190 if(IS_READY(serial->write_q))
192 if(IS_READY(serial->wait_q))
195 /* fprintf(stderr,"poll events are %04x\n",events); */
200 static int serial_get_fd( struct object *obj )
202 struct serial *serial = (struct serial *)obj;
203 assert( obj->ops == &serial_ops );
204 return serial->obj.fd;
207 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
209 struct serial *serial = (struct serial *) obj;
210 assert( obj->ops == &serial_ops );
214 reply->type = FILE_TYPE_CHAR;
216 reply->access_time = 0;
217 reply->write_time = 0;
218 reply->size_high = 0;
221 reply->index_high = 0;
222 reply->index_low = 0;
227 if(serial->attrib & FILE_FLAG_OVERLAPPED)
228 *flags |= FD_FLAG_OVERLAPPED;
229 else if(!((serial->readinterval == MAXDWORD) &&
230 (serial->readmult == 0) && (serial->readconst == 0)) )
231 *flags |= FD_FLAG_TIMEOUT;
233 return FD_TYPE_DEFAULT;
236 static void serial_poll_event(struct object *obj, int event)
238 struct serial *serial = (struct serial *)obj;
240 /* fprintf(stderr,"Poll event %02x\n",event); */
242 if(IS_READY(serial->read_q) && (POLLIN & event) )
243 async_notify(serial->read_q.head,STATUS_ALERTED);
245 if(IS_READY(serial->write_q) && (POLLOUT & event) )
246 async_notify(serial->write_q.head,STATUS_ALERTED);
248 if(IS_READY(serial->wait_q) && (POLLIN & event) )
249 async_notify(serial->wait_q.head,STATUS_ALERTED);
251 set_select_events(obj,obj->ops->get_poll_events(obj));
254 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
256 struct serial *serial = (struct serial *)obj;
257 struct async_queue *q;
261 assert(obj->ops == &serial_ops);
265 case ASYNC_TYPE_READ:
267 timeout = serial->readconst + serial->readmult*count;
269 case ASYNC_TYPE_WAIT:
273 case ASYNC_TYPE_WRITE:
274 q = &serial->write_q;
275 timeout = serial->writeconst + serial->writemult*count;
278 set_error(STATUS_INVALID_PARAMETER);
282 async = find_async ( q, current, ptr );
284 if ( status == STATUS_PENDING )
289 async = create_async ( obj, current, ptr );
293 async->status = STATUS_PENDING;
296 async_add_timeout(async,timeout);
297 async_insert(q, async);
300 /* Check if the new pending request can be served immediately */
302 pfd.events = serial_get_poll_events ( obj );
307 /* serial_poll_event() calls set_select_events() */
308 serial_poll_event ( obj, pfd.revents );
310 set_select_events ( obj, pfd.events );
313 else if ( async ) destroy_async ( async );
314 else set_error ( STATUS_INVALID_PARAMETER );
316 set_select_events ( obj, serial_get_poll_events ( obj ));
319 /* create a serial */
320 DECL_HANDLER(create_serial)
322 struct serial *serial;
325 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
327 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
328 release_object( serial );
332 DECL_HANDLER(get_serial_info)
334 struct serial *serial;
336 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
339 reply->readinterval = serial->readinterval;
340 reply->readconst = serial->readconst;
341 reply->readmult = serial->readmult;
342 reply->writeconst = serial->writeconst;
343 reply->writemult = serial->writemult;
346 reply->eventmask = serial->eventmask;
348 /* comm port error status */
349 reply->commerror = serial->commerror;
351 release_object( serial );
355 DECL_HANDLER(set_serial_info)
357 struct serial *serial;
359 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
362 if(req->flags & SERIALINFO_SET_TIMEOUTS)
364 serial->readinterval = req->readinterval;
365 serial->readconst = req->readconst;
366 serial->readmult = req->readmult;
367 serial->writeconst = req->writeconst;
368 serial->writemult = req->writemult;
372 if(req->flags & SERIALINFO_SET_MASK)
374 serial->eventmask = req->eventmask;
375 if(!serial->eventmask)
377 while(serial->wait_q.head)
379 async_notify(serial->wait_q.head, STATUS_SUCCESS);
380 destroy_async(serial->wait_q.head);
385 /* comm port error status */
386 if(req->flags & SERIALINFO_SET_ERROR)
388 serial->commerror = req->commerror;
391 release_object( serial );