blob: 486b5abb477b4226cc0e8410423f439d7a84e881 [file] [log] [blame]
Mike McCormack44b5bf52000-09-07 18:39:51 +00001/*
2 * Server-side serial port communications management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Mike McCormack6f011c02001-12-20 00:07:05 +00005 * Copyright (C) 2000,2001 Mike McCormack
Mike McCormack44b5bf52000-09-07 18:39:51 +00006 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00007 *
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.
12 *
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.
17 *
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
Mike McCormack44b5bf52000-09-07 18:39:51 +000021 */
22
23#include "config.h"
Martin Wilckff1f3202002-04-26 18:31:19 +000024#include "wine/port.h"
Mike McCormack44b5bf52000-09-07 18:39:51 +000025
26#include <assert.h>
27#include <fcntl.h>
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <errno.h>
32#ifdef HAVE_SYS_ERRNO_H
33#include <sys/errno.h>
34#endif
Mike McCormack44b5bf52000-09-07 18:39:51 +000035#include <sys/time.h>
36#include <sys/types.h>
37#include <time.h>
38#include <unistd.h>
39#include <utime.h>
40#include <termios.h>
41#include <sys/ioctl.h>
42
43#include "winerror.h"
44#include "winbase.h"
45
46#include "handle.h"
47#include "thread.h"
48#include "request.h"
Mike McCormack6f011c02001-12-20 00:07:05 +000049#include "async.h"
Mike McCormack44b5bf52000-09-07 18:39:51 +000050
51static void serial_dump( struct object *obj, int verbose );
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000052static int serial_get_fd( struct object *obj );
Martin Wilck88cd32b2002-01-09 20:30:51 +000053static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
Mike McCormack44b5bf52000-09-07 18:39:51 +000054static int serial_get_poll_events( struct object *obj );
Martin Wilck54ba2722002-04-24 21:29:54 +000055static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
Mike McCormack6f011c02001-12-20 00:07:05 +000056static void destroy_serial(struct object *obj);
57static void serial_poll_event( struct object *obj, int event );
Mike McCormack44b5bf52000-09-07 18:39:51 +000058
59struct serial
60{
61 struct object obj;
Alexandre Julliard57f05e12000-10-15 00:40:25 +000062 unsigned int access;
Mike McCormack568c67e2001-10-08 20:40:57 +000063 unsigned int attrib;
Mike McCormack44b5bf52000-09-07 18:39:51 +000064
65 /* timeout values */
66 unsigned int readinterval;
67 unsigned int readconst;
68 unsigned int readmult;
69 unsigned int writeconst;
70 unsigned int writemult;
71
72 unsigned int eventmask;
73 unsigned int commerror;
74
75 struct termios original;
76
Mike McCormack6f011c02001-12-20 00:07:05 +000077 struct async_queue read_q;
78 struct async_queue write_q;
79 struct async_queue wait_q;
80
Mike McCormack44b5bf52000-09-07 18:39:51 +000081 /* FIXME: add dcb, comm status, handler module, sharing */
82};
83
84static const struct object_ops serial_ops =
85{
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 */
Mike McCormack6f011c02001-12-20 00:07:05 +000093 serial_poll_event, /* poll_event */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000094 serial_get_fd, /* get_fd */
Mike McCormack44b5bf52000-09-07 18:39:51 +000095 no_flush, /* flush */
96 serial_get_info, /* get_file_info */
Mike McCormack6f011c02001-12-20 00:07:05 +000097 serial_queue_async, /* queue_async */
98 destroy_serial /* destroy */
Mike McCormack44b5bf52000-09-07 18:39:51 +000099};
100
Mike McCormack568c67e2001-10-08 20:40:57 +0000101static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000102{
103 struct serial *serial;
104 struct termios tios;
105 int fd, flags = 0;
106 char *name;
107
108 if (!(name = mem_alloc( len + 1 ))) return NULL;
109 memcpy( name, nameptr, len );
110 name[len] = 0;
111
112 switch(access & (GENERIC_READ | GENERIC_WRITE))
113 {
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;
117 default: break;
118 }
119
Mike McCormack05b66182001-08-21 17:01:48 +0000120 flags |= O_NONBLOCK;
121
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000122 fd = open( name, flags );
123 free( name );
124 if (fd < 0)
125 {
126 file_set_error();
127 return NULL;
128 }
129
130 /* check its really a serial port */
131 if (tcgetattr(fd,&tios))
132 {
133 file_set_error();
134 close( fd );
135 return NULL;
136 }
137
Mike McCormack568c67e2001-10-08 20:40:57 +0000138 /* set the fd back to blocking if necessary */
139 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
140 if(0>fcntl(fd, F_SETFL, 0))
141 perror("fcntl");
142
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000143 if ((serial = alloc_object( &serial_ops, fd )))
144 {
Mike McCormack568c67e2001-10-08 20:40:57 +0000145 serial->attrib = attributes;
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000146 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;
Mike McCormack6f011c02001-12-20 00:07:05 +0000154 init_async_queue(&serial->read_q);
155 init_async_queue(&serial->write_q);
156 init_async_queue(&serial->wait_q);
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000157 }
158 return serial;
159}
160
Mike McCormack6f011c02001-12-20 00:07:05 +0000161static void destroy_serial( struct object *obj)
162{
163 struct serial *serial = (struct serial *)obj;
164
165 destroy_async_queue(&serial->read_q);
166 destroy_async_queue(&serial->write_q);
167 destroy_async_queue(&serial->wait_q);
168}
169
Mike McCormack44b5bf52000-09-07 18:39:51 +0000170static void serial_dump( struct object *obj, int verbose )
171{
172 struct serial *serial = (struct serial *)obj;
173 assert( obj->ops == &serial_ops );
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000174 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
Mike McCormack44b5bf52000-09-07 18:39:51 +0000175}
176
Alexandre Julliard51885742002-05-30 20:12:58 +0000177struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000178{
179 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
180}
181
182static int serial_get_poll_events( struct object *obj )
183{
184 struct serial *serial = (struct serial *)obj;
185 int events = 0;
186 assert( obj->ops == &serial_ops );
Mike McCormack6f011c02001-12-20 00:07:05 +0000187
188 if(IS_READY(serial->read_q))
189 events |= POLLIN;
190 if(IS_READY(serial->write_q))
191 events |= POLLOUT;
192 if(IS_READY(serial->wait_q))
193 events |= POLLIN;
194
195 /* fprintf(stderr,"poll events are %04x\n",events); */
196
Mike McCormack44b5bf52000-09-07 18:39:51 +0000197 return events;
198}
199
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000200static int serial_get_fd( struct object *obj )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000201{
202 struct serial *serial = (struct serial *)obj;
203 assert( obj->ops == &serial_ops );
Alexandre Julliard63411db2000-12-22 21:12:36 +0000204 return serial->obj.fd;
Mike McCormack44b5bf52000-09-07 18:39:51 +0000205}
206
Martin Wilck88cd32b2002-01-09 20:30:51 +0000207static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000208{
Mike McCormack568c67e2001-10-08 20:40:57 +0000209 struct serial *serial = (struct serial *) obj;
210 assert( obj->ops == &serial_ops );
211
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000212 if (reply)
Mike McCormackff58be52001-10-04 16:18:15 +0000213 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000214 reply->type = FILE_TYPE_CHAR;
215 reply->attr = 0;
216 reply->access_time = 0;
217 reply->write_time = 0;
218 reply->size_high = 0;
219 reply->size_low = 0;
220 reply->links = 0;
221 reply->index_high = 0;
222 reply->index_low = 0;
223 reply->serial = 0;
Mike McCormackff58be52001-10-04 16:18:15 +0000224 }
Mike McCormack568c67e2001-10-08 20:40:57 +0000225
Martin Wilck88cd32b2002-01-09 20:30:51 +0000226 *flags = 0;
Mike McCormack568c67e2001-10-08 20:40:57 +0000227 if(serial->attrib & FILE_FLAG_OVERLAPPED)
Martin Wilck88cd32b2002-01-09 20:30:51 +0000228 *flags |= FD_FLAG_OVERLAPPED;
229 else if(!((serial->readinterval == MAXDWORD) &&
230 (serial->readmult == 0) && (serial->readconst == 0)) )
231 *flags |= FD_FLAG_TIMEOUT;
Mike McCormack568c67e2001-10-08 20:40:57 +0000232
Martin Wilck88cd32b2002-01-09 20:30:51 +0000233 return FD_TYPE_DEFAULT;
Mike McCormack44b5bf52000-09-07 18:39:51 +0000234}
235
Mike McCormack6f011c02001-12-20 00:07:05 +0000236static void serial_poll_event(struct object *obj, int event)
Mike McCormack1eac1912000-11-13 19:27:21 +0000237{
238 struct serial *serial = (struct serial *)obj;
Mike McCormack1eac1912000-11-13 19:27:21 +0000239
Mike McCormack6f011c02001-12-20 00:07:05 +0000240 /* fprintf(stderr,"Poll event %02x\n",event); */
241
242 if(IS_READY(serial->read_q) && (POLLIN & event) )
243 async_notify(serial->read_q.head,STATUS_ALERTED);
244
245 if(IS_READY(serial->write_q) && (POLLOUT & event) )
246 async_notify(serial->write_q.head,STATUS_ALERTED);
247
248 if(IS_READY(serial->wait_q) && (POLLIN & event) )
249 async_notify(serial->wait_q.head,STATUS_ALERTED);
250
251 set_select_events(obj,obj->ops->get_poll_events(obj));
252}
253
Martin Wilck54ba2722002-04-24 21:29:54 +0000254static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
Mike McCormack6f011c02001-12-20 00:07:05 +0000255{
256 struct serial *serial = (struct serial *)obj;
257 struct async_queue *q;
Martin Wilck54ba2722002-04-24 21:29:54 +0000258 struct async *async;
Mike McCormack6f011c02001-12-20 00:07:05 +0000259 int timeout;
260
261 assert(obj->ops == &serial_ops);
Mike McCormack1eac1912000-11-13 19:27:21 +0000262
Michael McCormack1c32a462001-03-22 20:09:34 +0000263 switch(type)
Mike McCormack1eac1912000-11-13 19:27:21 +0000264 {
265 case ASYNC_TYPE_READ:
Mike McCormack6f011c02001-12-20 00:07:05 +0000266 q = &serial->read_q;
267 timeout = serial->readconst + serial->readmult*count;
268 break;
269 case ASYNC_TYPE_WAIT:
270 q = &serial->wait_q;
271 timeout = 0;
272 break;
Mike McCormack1eac1912000-11-13 19:27:21 +0000273 case ASYNC_TYPE_WRITE:
Mike McCormack6f011c02001-12-20 00:07:05 +0000274 q = &serial->write_q;
275 timeout = serial->writeconst + serial->writemult*count;
276 break;
277 default:
278 set_error(STATUS_INVALID_PARAMETER);
Martin Wilck54ba2722002-04-24 21:29:54 +0000279 return;
Mike McCormack1eac1912000-11-13 19:27:21 +0000280 }
Mike McCormack6f011c02001-12-20 00:07:05 +0000281
Martin Wilck54ba2722002-04-24 21:29:54 +0000282 async = find_async ( q, current, ptr );
283
284 if ( status == STATUS_PENDING )
Mike McCormack6f011c02001-12-20 00:07:05 +0000285 {
Martin Wilckff1f3202002-04-26 18:31:19 +0000286 struct pollfd pfd;
287
Martin Wilck54ba2722002-04-24 21:29:54 +0000288 if ( !async )
289 async = create_async ( obj, current, ptr );
290 if ( !async )
291 return;
292
293 async->status = STATUS_PENDING;
Mike McCormack6f011c02001-12-20 00:07:05 +0000294 if(!async->q)
295 {
296 async_add_timeout(async,timeout);
297 async_insert(q, async);
Martin Wilck54ba2722002-04-24 21:29:54 +0000298 }
Martin Wilckff1f3202002-04-26 18:31:19 +0000299
300 /* Check if the new pending request can be served immediately */
301 pfd.fd = obj->fd;
302 pfd.events = serial_get_poll_events ( obj );
303 pfd.revents = 0;
304 poll ( &pfd, 1, 0 );
305
306 if ( pfd.revents )
307 /* serial_poll_event() calls set_select_events() */
308 serial_poll_event ( obj, pfd.revents );
309 else
310 set_select_events ( obj, pfd.events );
311 return;
Mike McCormack6f011c02001-12-20 00:07:05 +0000312 }
Martin Wilck54ba2722002-04-24 21:29:54 +0000313 else if ( async ) destroy_async ( async );
314 else set_error ( STATUS_INVALID_PARAMETER );
Mike McCormack1eac1912000-11-13 19:27:21 +0000315
Martin Wilck54ba2722002-04-24 21:29:54 +0000316 set_select_events ( obj, serial_get_poll_events ( obj ));
Mike McCormack6f011c02001-12-20 00:07:05 +0000317}
Mike McCormack1eac1912000-11-13 19:27:21 +0000318
Mike McCormack44b5bf52000-09-07 18:39:51 +0000319/* create a serial */
320DECL_HANDLER(create_serial)
321{
322 struct serial *serial;
Mike McCormack44b5bf52000-09-07 18:39:51 +0000323
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000324 reply->handle = 0;
325 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
Mike McCormack44b5bf52000-09-07 18:39:51 +0000326 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000327 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
Mike McCormack44b5bf52000-09-07 18:39:51 +0000328 release_object( serial );
329 }
330}
331
Mike McCormack654fcc72000-09-16 20:55:12 +0000332DECL_HANDLER(get_serial_info)
333{
334 struct serial *serial;
335
336 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
337 {
338 /* timeouts */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000339 reply->readinterval = serial->readinterval;
340 reply->readconst = serial->readconst;
341 reply->readmult = serial->readmult;
342 reply->writeconst = serial->writeconst;
343 reply->writemult = serial->writemult;
Mike McCormack654fcc72000-09-16 20:55:12 +0000344
345 /* event mask */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000346 reply->eventmask = serial->eventmask;
Mike McCormack654fcc72000-09-16 20:55:12 +0000347
348 /* comm port error status */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000349 reply->commerror = serial->commerror;
Mike McCormack654fcc72000-09-16 20:55:12 +0000350
351 release_object( serial );
352 }
353}
354
355DECL_HANDLER(set_serial_info)
356{
357 struct serial *serial;
358
359 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
360 {
361 /* timeouts */
362 if(req->flags & SERIALINFO_SET_TIMEOUTS)
363 {
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;
369 }
370
371 /* event mask */
372 if(req->flags & SERIALINFO_SET_MASK)
373 {
374 serial->eventmask = req->eventmask;
Mike McCormack32521ab2002-03-12 19:19:57 +0000375 if(!serial->eventmask)
376 {
377 while(serial->wait_q.head)
378 {
379 async_notify(serial->wait_q.head, STATUS_SUCCESS);
380 destroy_async(serial->wait_q.head);
381 }
382 }
Mike McCormack654fcc72000-09-16 20:55:12 +0000383 }
384
385 /* comm port error status */
386 if(req->flags & SERIALINFO_SET_ERROR)
387 {
388 serial->commerror = req->commerror;
389 }
390
391 release_object( serial );
392 }
393}
394