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 struct async_queue * serial_queue_async(struct object *obj, struct async* async, 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));
254 * This function is an abuse of overloading that deserves some explanation.
256 * It has three purposes:
258 * 1. get the queue for a type of async operation
259 * 2. requeue an async operation
260 * 3. queue a new async operation
262 * It is overloaded so that these three functions only take one function pointer
263 * in the object operations list.
265 * In all cases, it returns the async queue.
267 static struct async_queue *serial_queue_async(struct object *obj, struct async *async, int type, int count)
269 struct serial *serial = (struct serial *)obj;
270 struct async_queue *q;
273 assert(obj->ops == &serial_ops);
277 case ASYNC_TYPE_READ:
279 timeout = serial->readconst + serial->readmult*count;
281 case ASYNC_TYPE_WAIT:
285 case ASYNC_TYPE_WRITE:
286 q = &serial->write_q;
287 timeout = serial->writeconst + serial->writemult*count;
290 set_error(STATUS_INVALID_PARAMETER);
298 async_add_timeout(async,timeout);
299 async_insert(q, async);
306 /* create a serial */
307 DECL_HANDLER(create_serial)
309 struct serial *serial;
312 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
314 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
315 release_object( serial );
319 DECL_HANDLER(get_serial_info)
321 struct serial *serial;
323 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
326 reply->readinterval = serial->readinterval;
327 reply->readconst = serial->readconst;
328 reply->readmult = serial->readmult;
329 reply->writeconst = serial->writeconst;
330 reply->writemult = serial->writemult;
333 reply->eventmask = serial->eventmask;
335 /* comm port error status */
336 reply->commerror = serial->commerror;
338 release_object( serial );
342 DECL_HANDLER(set_serial_info)
344 struct serial *serial;
346 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
349 if(req->flags & SERIALINFO_SET_TIMEOUTS)
351 serial->readinterval = req->readinterval;
352 serial->readconst = req->readconst;
353 serial->readmult = req->readmult;
354 serial->writeconst = req->writeconst;
355 serial->writemult = req->writemult;
359 if(req->flags & SERIALINFO_SET_MASK)
361 serial->eventmask = req->eventmask;
362 if(!serial->eventmask)
364 while(serial->wait_q.head)
366 async_notify(serial->wait_q.head, STATUS_SUCCESS);
367 destroy_async(serial->wait_q.head);
372 /* comm port error status */
373 if(req->flags & SERIALINFO_SET_ERROR)
375 serial->commerror = req->commerror;
378 release_object( serial );