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>
47 static void serial_dump( struct object *obj, int verbose );
48 static int serial_get_fd( struct object *obj );
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_poll_add_queue, /* add_queue */
86 default_poll_remove_queue, /* remove_queue */
87 default_poll_signaled, /* signaled */
88 no_satisfied, /* satisfied */
89 serial_get_poll_events, /* get_poll_events */
90 serial_poll_event, /* poll_event */
91 serial_get_fd, /* get_fd */
92 serial_flush, /* flush */
93 serial_get_info, /* get_file_info */
94 serial_queue_async, /* queue_async */
95 destroy_serial /* destroy */
98 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
100 struct serial *serial;
105 if (!(name = mem_alloc( len + 1 ))) return NULL;
106 memcpy( name, nameptr, len );
109 switch(access & (GENERIC_READ | GENERIC_WRITE))
111 case GENERIC_READ: flags |= O_RDONLY; break;
112 case GENERIC_WRITE: flags |= O_WRONLY; break;
113 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
119 fd = open( name, flags );
127 /* check its really a serial port */
128 if (tcgetattr(fd,&tios))
135 /* set the fd back to blocking if necessary */
136 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
137 if(0>fcntl(fd, F_SETFL, 0))
140 if ((serial = alloc_object( &serial_ops, fd )))
142 serial->attrib = attributes;
143 serial->access = access;
144 serial->readinterval = 0;
145 serial->readmult = 0;
146 serial->readconst = 0;
147 serial->writemult = 0;
148 serial->writeconst = 0;
149 serial->eventmask = 0;
150 serial->commerror = 0;
151 init_async_queue(&serial->read_q);
152 init_async_queue(&serial->write_q);
153 init_async_queue(&serial->wait_q);
158 static void destroy_serial( struct object *obj)
160 struct serial *serial = (struct serial *)obj;
162 destroy_async_queue(&serial->read_q);
163 destroy_async_queue(&serial->write_q);
164 destroy_async_queue(&serial->wait_q);
167 static void serial_dump( struct object *obj, int verbose )
169 struct serial *serial = (struct serial *)obj;
170 assert( obj->ops == &serial_ops );
171 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
174 struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
176 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
179 static int serial_get_poll_events( struct object *obj )
181 struct serial *serial = (struct serial *)obj;
183 assert( obj->ops == &serial_ops );
185 if(IS_READY(serial->read_q))
187 if(IS_READY(serial->write_q))
189 if(IS_READY(serial->wait_q))
192 /* fprintf(stderr,"poll events are %04x\n",events); */
197 static int serial_get_fd( struct object *obj )
199 struct serial *serial = (struct serial *)obj;
200 assert( obj->ops == &serial_ops );
201 return serial->obj.fd;
204 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
206 struct serial *serial = (struct serial *) obj;
207 assert( obj->ops == &serial_ops );
211 reply->type = FILE_TYPE_CHAR;
213 reply->access_time = 0;
214 reply->write_time = 0;
215 reply->size_high = 0;
218 reply->index_high = 0;
219 reply->index_low = 0;
224 if(serial->attrib & FILE_FLAG_OVERLAPPED)
225 *flags |= FD_FLAG_OVERLAPPED;
226 else if(!((serial->readinterval == MAXDWORD) &&
227 (serial->readmult == 0) && (serial->readconst == 0)) )
228 *flags |= FD_FLAG_TIMEOUT;
230 return FD_TYPE_DEFAULT;
233 static void serial_poll_event(struct object *obj, int event)
235 struct serial *serial = (struct serial *)obj;
237 /* fprintf(stderr,"Poll event %02x\n",event); */
239 if(IS_READY(serial->read_q) && (POLLIN & event) )
240 async_notify(serial->read_q.head,STATUS_ALERTED);
242 if(IS_READY(serial->write_q) && (POLLOUT & event) )
243 async_notify(serial->write_q.head,STATUS_ALERTED);
245 if(IS_READY(serial->wait_q) && (POLLIN & event) )
246 async_notify(serial->wait_q.head,STATUS_ALERTED);
248 set_select_events(obj,obj->ops->get_poll_events(obj));
251 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
253 struct serial *serial = (struct serial *)obj;
254 struct async_queue *q;
258 assert(obj->ops == &serial_ops);
262 case ASYNC_TYPE_READ:
264 timeout = serial->readconst + serial->readmult*count;
266 case ASYNC_TYPE_WAIT:
270 case ASYNC_TYPE_WRITE:
271 q = &serial->write_q;
272 timeout = serial->writeconst + serial->writemult*count;
275 set_error(STATUS_INVALID_PARAMETER);
279 async = find_async ( q, current, ptr );
281 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 /* Check if the new pending request can be served immediately */
299 pfd.events = serial_get_poll_events ( obj );
304 /* serial_poll_event() calls set_select_events() */
305 serial_poll_event ( obj, pfd.revents );
307 set_select_events ( obj, pfd.events );
310 else if ( async ) destroy_async ( async );
311 else set_error ( STATUS_INVALID_PARAMETER );
313 set_select_events ( obj, serial_get_poll_events ( obj ));
316 static int serial_flush( struct object *obj )
319 struct serial *serial = (struct serial *)grab_object(obj);
320 assert( obj->ops == &serial_ops );
322 /* MSDN says: If hFile is a handle to a communications device,
323 * the function only flushes the transmit buffer.
325 ret = (tcflush( serial->obj.fd, TCOFLUSH ) != -1);
326 if (!ret) file_set_error();
327 release_object( serial );
331 /* create a serial */
332 DECL_HANDLER(create_serial)
334 struct serial *serial;
337 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
339 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
340 release_object( serial );
344 DECL_HANDLER(get_serial_info)
346 struct serial *serial;
348 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
351 reply->readinterval = serial->readinterval;
352 reply->readconst = serial->readconst;
353 reply->readmult = serial->readmult;
354 reply->writeconst = serial->writeconst;
355 reply->writemult = serial->writemult;
358 reply->eventmask = serial->eventmask;
360 /* comm port error status */
361 reply->commerror = serial->commerror;
363 release_object( serial );
367 DECL_HANDLER(set_serial_info)
369 struct serial *serial;
371 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
374 if(req->flags & SERIALINFO_SET_TIMEOUTS)
376 serial->readinterval = req->readinterval;
377 serial->readconst = req->readconst;
378 serial->readmult = req->readmult;
379 serial->writeconst = req->writeconst;
380 serial->writemult = req->writemult;
384 if(req->flags & SERIALINFO_SET_MASK)
386 serial->eventmask = req->eventmask;
387 if(!serial->eventmask)
389 while(serial->wait_q.head)
391 async_notify(serial->wait_q.head, STATUS_SUCCESS);
392 destroy_async(serial->wait_q.head);
397 /* comm port error status */
398 if(req->flags & SERIALINFO_SET_ERROR)
400 serial->commerror = req->commerror;
403 release_object( serial );