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>
37 #include <sys/ioctl.h>
48 static void serial_dump( struct object *obj, int verbose );
49 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
50 static int serial_get_poll_events( struct object *obj );
51 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
52 static void destroy_serial(struct object *obj);
53 static void serial_poll_event( struct object *obj, int event );
54 static int serial_flush( struct object *obj );
63 unsigned int readinterval;
64 unsigned int readconst;
65 unsigned int readmult;
66 unsigned int writeconst;
67 unsigned int writemult;
69 unsigned int eventmask;
70 unsigned int commerror;
72 struct termios original;
74 struct async_queue read_q;
75 struct async_queue write_q;
76 struct async_queue wait_q;
78 /* FIXME: add dcb, comm status, handler module, sharing */
81 static const struct object_ops serial_ops =
83 sizeof(struct serial), /* size */
84 serial_dump, /* dump */
85 default_fd_add_queue, /* add_queue */
86 default_fd_remove_queue, /* remove_queue */
87 default_fd_signaled, /* signaled */
88 no_satisfied, /* satisfied */
89 default_get_fd, /* get_fd */
90 serial_get_info, /* get_file_info */
91 destroy_serial /* destroy */
94 static const struct fd_ops serial_fd_ops =
96 serial_get_poll_events, /* get_poll_events */
97 serial_poll_event, /* poll_event */
98 serial_flush, /* flush */
99 serial_get_info, /* get_file_info */
100 serial_queue_async /* queue_async */
103 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
105 struct serial *serial;
110 if (!(name = mem_alloc( len + 1 ))) return NULL;
111 memcpy( name, nameptr, len );
114 switch(access & (GENERIC_READ | GENERIC_WRITE))
116 case GENERIC_READ: flags |= O_RDONLY; break;
117 case GENERIC_WRITE: flags |= O_WRONLY; break;
118 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
124 fd = open( name, flags );
132 /* check its really a serial port */
133 if (tcgetattr(fd,&tios))
140 /* set the fd back to blocking if necessary */
141 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
142 if(0>fcntl(fd, F_SETFL, 0))
145 if ((serial = alloc_fd_object( &serial_ops, &serial_fd_ops, fd )))
147 serial->attrib = attributes;
148 serial->access = access;
149 serial->readinterval = 0;
150 serial->readmult = 0;
151 serial->readconst = 0;
152 serial->writemult = 0;
153 serial->writeconst = 0;
154 serial->eventmask = 0;
155 serial->commerror = 0;
156 init_async_queue(&serial->read_q);
157 init_async_queue(&serial->write_q);
158 init_async_queue(&serial->wait_q);
163 static void destroy_serial( struct object *obj)
165 struct serial *serial = (struct serial *)obj;
167 destroy_async_queue(&serial->read_q);
168 destroy_async_queue(&serial->write_q);
169 destroy_async_queue(&serial->wait_q);
172 static void serial_dump( struct object *obj, int verbose )
174 struct serial *serial = (struct serial *)obj;
175 assert( obj->ops == &serial_ops );
176 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
179 struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
181 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
184 static int serial_get_poll_events( struct object *obj )
186 struct serial *serial = (struct serial *)obj;
188 assert( obj->ops == &serial_ops );
190 if(IS_READY(serial->read_q))
192 if(IS_READY(serial->write_q))
194 if(IS_READY(serial->wait_q))
197 /* fprintf(stderr,"poll events are %04x\n",events); */
202 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
204 struct serial *serial = (struct serial *) obj;
205 assert( obj->ops == &serial_ops );
209 reply->type = FILE_TYPE_CHAR;
211 reply->access_time = 0;
212 reply->write_time = 0;
213 reply->size_high = 0;
216 reply->index_high = 0;
217 reply->index_low = 0;
222 if(serial->attrib & FILE_FLAG_OVERLAPPED)
223 *flags |= FD_FLAG_OVERLAPPED;
224 else if(!((serial->readinterval == MAXDWORD) &&
225 (serial->readmult == 0) && (serial->readconst == 0)) )
226 *flags |= FD_FLAG_TIMEOUT;
228 return FD_TYPE_DEFAULT;
231 static void serial_poll_event(struct object *obj, int event)
233 struct serial *serial = (struct serial *)obj;
235 /* fprintf(stderr,"Poll event %02x\n",event); */
237 if(IS_READY(serial->read_q) && (POLLIN & event) )
238 async_notify(serial->read_q.head,STATUS_ALERTED);
240 if(IS_READY(serial->write_q) && (POLLOUT & event) )
241 async_notify(serial->write_q.head,STATUS_ALERTED);
243 if(IS_READY(serial->wait_q) && (POLLIN & event) )
244 async_notify(serial->wait_q.head,STATUS_ALERTED);
246 set_select_events( obj, serial_get_poll_events(obj) );
249 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
251 struct serial *serial = (struct serial *)obj;
252 struct async_queue *q;
256 assert(obj->ops == &serial_ops);
260 case ASYNC_TYPE_READ:
262 timeout = serial->readconst + serial->readmult*count;
264 case ASYNC_TYPE_WAIT:
268 case ASYNC_TYPE_WRITE:
269 q = &serial->write_q;
270 timeout = serial->writeconst + serial->writemult*count;
273 set_error(STATUS_INVALID_PARAMETER);
277 async = find_async ( q, current, ptr );
279 if ( status == STATUS_PENDING )
284 async = create_async ( obj, current, ptr );
288 async->status = STATUS_PENDING;
291 async_add_timeout(async,timeout);
292 async_insert(q, async);
295 /* Check if the new pending request can be served immediately */
296 pfd.fd = get_unix_fd( obj );
297 pfd.events = serial_get_poll_events ( obj );
302 /* serial_poll_event() calls set_select_events() */
303 serial_poll_event ( obj, pfd.revents );
305 set_select_events ( obj, pfd.events );
308 else if ( async ) destroy_async ( async );
309 else set_error ( STATUS_INVALID_PARAMETER );
311 set_select_events ( obj, serial_get_poll_events ( obj ));
314 static int serial_flush( struct object *obj )
316 /* MSDN says: If hFile is a handle to a communications device,
317 * the function only flushes the transmit buffer.
319 int ret = (tcflush( get_unix_fd(obj), TCOFLUSH ) != -1);
320 if (!ret) file_set_error();
324 /* create a serial */
325 DECL_HANDLER(create_serial)
327 struct serial *serial;
330 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
332 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
333 release_object( serial );
337 DECL_HANDLER(get_serial_info)
339 struct serial *serial;
341 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
344 reply->readinterval = serial->readinterval;
345 reply->readconst = serial->readconst;
346 reply->readmult = serial->readmult;
347 reply->writeconst = serial->writeconst;
348 reply->writemult = serial->writemult;
351 reply->eventmask = serial->eventmask;
353 /* comm port error status */
354 reply->commerror = serial->commerror;
356 release_object( serial );
360 DECL_HANDLER(set_serial_info)
362 struct serial *serial;
364 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
367 if(req->flags & SERIALINFO_SET_TIMEOUTS)
369 serial->readinterval = req->readinterval;
370 serial->readconst = req->readconst;
371 serial->readmult = req->readmult;
372 serial->writeconst = req->writeconst;
373 serial->writemult = req->writemult;
377 if(req->flags & SERIALINFO_SET_MASK)
379 serial->eventmask = req->eventmask;
380 if(!serial->eventmask)
382 while(serial->wait_q.head)
384 async_notify(serial->wait_q.head, STATUS_SUCCESS);
385 destroy_async(serial->wait_q.head);
390 /* comm port error status */
391 if(req->flags & SERIALINFO_SET_ERROR)
393 serial->commerror = req->commerror;
396 release_object( serial );