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
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
35 #include <sys/types.h>
40 #include <sys/ioctl.h>
50 static void serial_dump( struct object *obj, int verbose );
51 static int serial_get_fd( struct object *obj );
52 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
53 static int serial_get_poll_events( struct object *obj );
54 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
55 static void destroy_serial(struct object *obj);
56 static void serial_poll_event( struct object *obj, int event );
65 unsigned int readinterval;
66 unsigned int readconst;
67 unsigned int readmult;
68 unsigned int writeconst;
69 unsigned int writemult;
71 unsigned int eventmask;
72 unsigned int commerror;
74 struct termios original;
76 struct async_queue read_q;
77 struct async_queue write_q;
78 struct async_queue wait_q;
80 /* FIXME: add dcb, comm status, handler module, sharing */
83 static const struct object_ops serial_ops =
85 sizeof(struct serial), /* size */
86 serial_dump, /* dump */
87 default_poll_add_queue, /* add_queue */
88 default_poll_remove_queue, /* remove_queue */
89 default_poll_signaled, /* signaled */
90 no_satisfied, /* satisfied */
91 serial_get_poll_events, /* get_poll_events */
92 serial_poll_event, /* poll_event */
93 serial_get_fd, /* get_fd */
95 serial_get_info, /* get_file_info */
96 serial_queue_async, /* queue_async */
97 destroy_serial /* destroy */
100 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
102 struct serial *serial;
107 if (!(name = mem_alloc( len + 1 ))) return NULL;
108 memcpy( name, nameptr, len );
111 switch(access & (GENERIC_READ | GENERIC_WRITE))
113 case GENERIC_READ: flags |= O_RDONLY; break;
114 case GENERIC_WRITE: flags |= O_WRONLY; break;
115 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
121 fd = open( name, flags );
129 /* check its really a serial port */
130 if (tcgetattr(fd,&tios))
137 /* set the fd back to blocking if necessary */
138 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
139 if(0>fcntl(fd, F_SETFL, 0))
142 if ((serial = alloc_object( &serial_ops, fd )))
144 serial->attrib = attributes;
145 serial->access = access;
146 serial->readinterval = 0;
147 serial->readmult = 0;
148 serial->readconst = 0;
149 serial->writemult = 0;
150 serial->writeconst = 0;
151 serial->eventmask = 0;
152 serial->commerror = 0;
153 init_async_queue(&serial->read_q);
154 init_async_queue(&serial->write_q);
155 init_async_queue(&serial->wait_q);
160 static void destroy_serial( struct object *obj)
162 struct serial *serial = (struct serial *)obj;
164 destroy_async_queue(&serial->read_q);
165 destroy_async_queue(&serial->write_q);
166 destroy_async_queue(&serial->wait_q);
169 static void serial_dump( struct object *obj, int verbose )
171 struct serial *serial = (struct serial *)obj;
172 assert( obj->ops == &serial_ops );
173 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
176 struct serial *get_serial_obj( struct process *process, handle_t handle, unsigned int access )
178 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
181 static int serial_get_poll_events( struct object *obj )
183 struct serial *serial = (struct serial *)obj;
185 assert( obj->ops == &serial_ops );
187 if(IS_READY(serial->read_q))
189 if(IS_READY(serial->write_q))
191 if(IS_READY(serial->wait_q))
194 /* fprintf(stderr,"poll events are %04x\n",events); */
199 static int serial_get_fd( struct object *obj )
201 struct serial *serial = (struct serial *)obj;
202 assert( obj->ops == &serial_ops );
203 return serial->obj.fd;
206 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
208 struct serial *serial = (struct serial *) obj;
209 assert( obj->ops == &serial_ops );
213 reply->type = FILE_TYPE_CHAR;
215 reply->access_time = 0;
216 reply->write_time = 0;
217 reply->size_high = 0;
220 reply->index_high = 0;
221 reply->index_low = 0;
226 if(serial->attrib & FILE_FLAG_OVERLAPPED)
227 *flags |= FD_FLAG_OVERLAPPED;
228 else if(!((serial->readinterval == MAXDWORD) &&
229 (serial->readmult == 0) && (serial->readconst == 0)) )
230 *flags |= FD_FLAG_TIMEOUT;
232 return FD_TYPE_DEFAULT;
235 static void serial_poll_event(struct object *obj, int event)
237 struct serial *serial = (struct serial *)obj;
239 /* fprintf(stderr,"Poll event %02x\n",event); */
241 if(IS_READY(serial->read_q) && (POLLIN & event) )
242 async_notify(serial->read_q.head,STATUS_ALERTED);
244 if(IS_READY(serial->write_q) && (POLLOUT & event) )
245 async_notify(serial->write_q.head,STATUS_ALERTED);
247 if(IS_READY(serial->wait_q) && (POLLIN & event) )
248 async_notify(serial->wait_q.head,STATUS_ALERTED);
250 set_select_events(obj,obj->ops->get_poll_events(obj));
253 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
255 struct serial *serial = (struct serial *)obj;
256 struct async_queue *q;
260 assert(obj->ops == &serial_ops);
264 case ASYNC_TYPE_READ:
266 timeout = serial->readconst + serial->readmult*count;
268 case ASYNC_TYPE_WAIT:
272 case ASYNC_TYPE_WRITE:
273 q = &serial->write_q;
274 timeout = serial->writeconst + serial->writemult*count;
277 set_error(STATUS_INVALID_PARAMETER);
281 async = find_async ( q, current, ptr );
283 if ( status == STATUS_PENDING )
286 async = create_async ( obj, current, ptr );
290 async->status = STATUS_PENDING;
293 async_add_timeout(async,timeout);
294 async_insert(q, async);
297 else if ( async ) destroy_async ( async );
298 else set_error ( STATUS_INVALID_PARAMETER );
300 set_select_events ( obj, serial_get_poll_events ( obj ));
303 /* create a serial */
304 DECL_HANDLER(create_serial)
306 struct serial *serial;
309 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
311 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
312 release_object( serial );
316 DECL_HANDLER(get_serial_info)
318 struct serial *serial;
320 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
323 reply->readinterval = serial->readinterval;
324 reply->readconst = serial->readconst;
325 reply->readmult = serial->readmult;
326 reply->writeconst = serial->writeconst;
327 reply->writemult = serial->writemult;
330 reply->eventmask = serial->eventmask;
332 /* comm port error status */
333 reply->commerror = serial->commerror;
335 release_object( serial );
339 DECL_HANDLER(set_serial_info)
341 struct serial *serial;
343 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
346 if(req->flags & SERIALINFO_SET_TIMEOUTS)
348 serial->readinterval = req->readinterval;
349 serial->readconst = req->readconst;
350 serial->readmult = req->readmult;
351 serial->writeconst = req->writeconst;
352 serial->writemult = req->writemult;
356 if(req->flags & SERIALINFO_SET_MASK)
358 serial->eventmask = req->eventmask;
359 if(!serial->eventmask)
361 while(serial->wait_q.head)
363 async_notify(serial->wait_q.head, STATUS_SUCCESS);
364 destroy_async(serial->wait_q.head);
369 /* comm port error status */
370 if(req->flags & SERIALINFO_SET_ERROR)
372 serial->commerror = req->commerror;
375 release_object( serial );