blob: 06b07cb032d8333d7771ec8ca25a223c9c95d848 [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>
Mike McCormack44b5bf52000-09-07 18:39:51 +000031#include <sys/time.h>
32#include <sys/types.h>
33#include <time.h>
34#include <unistd.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000035#ifdef HAVE_UTIME_H
Mike McCormack44b5bf52000-09-07 18:39:51 +000036#include <utime.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000037#endif
38#ifdef HAVE_TERMIOS_H
Mike McCormack44b5bf52000-09-07 18:39:51 +000039#include <termios.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000040#endif
41#ifdef HAVE_SYS_IOCTL_H
Mike McCormack44b5bf52000-09-07 18:39:51 +000042#include <sys/ioctl.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000043#endif
Mike McCormack44b5bf52000-09-07 18:39:51 +000044
45#include "winerror.h"
46#include "winbase.h"
47
Alexandre Julliard863637b2003-01-30 00:26:44 +000048#include "file.h"
Mike McCormack44b5bf52000-09-07 18:39:51 +000049#include "handle.h"
50#include "thread.h"
51#include "request.h"
Mike McCormack6f011c02001-12-20 00:07:05 +000052#include "async.h"
Mike McCormack44b5bf52000-09-07 18:39:51 +000053
54static void serial_dump( struct object *obj, int verbose );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000055static void serial_destroy(struct object *obj);
56
57static int serial_get_poll_events( struct fd *fd );
58static void serial_poll_event( struct fd *fd, int event );
59static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
60static int serial_flush( struct fd *fd );
61static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count);
Mike McCormack44b5bf52000-09-07 18:39:51 +000062
63struct serial
64{
65 struct object obj;
Alexandre Julliard57f05e12000-10-15 00:40:25 +000066 unsigned int access;
Mike McCormack568c67e2001-10-08 20:40:57 +000067 unsigned int attrib;
Mike McCormack44b5bf52000-09-07 18:39:51 +000068
69 /* timeout values */
70 unsigned int readinterval;
71 unsigned int readconst;
72 unsigned int readmult;
73 unsigned int writeconst;
74 unsigned int writemult;
75
76 unsigned int eventmask;
77 unsigned int commerror;
78
79 struct termios original;
80
Mike McCormack6f011c02001-12-20 00:07:05 +000081 struct async_queue read_q;
82 struct async_queue write_q;
83 struct async_queue wait_q;
84
Mike McCormack44b5bf52000-09-07 18:39:51 +000085 /* FIXME: add dcb, comm status, handler module, sharing */
86};
87
88static const struct object_ops serial_ops =
89{
90 sizeof(struct serial), /* size */
91 serial_dump, /* dump */
Alexandre Julliard863637b2003-01-30 00:26:44 +000092 default_fd_add_queue, /* add_queue */
93 default_fd_remove_queue, /* remove_queue */
94 default_fd_signaled, /* signaled */
Mike McCormack44b5bf52000-09-07 18:39:51 +000095 no_satisfied, /* satisfied */
Alexandre Julliard863637b2003-01-30 00:26:44 +000096 default_get_fd, /* get_fd */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000097 serial_destroy /* destroy */
Alexandre Julliard863637b2003-01-30 00:26:44 +000098};
99
100static const struct fd_ops serial_fd_ops =
101{
Mike McCormack44b5bf52000-09-07 18:39:51 +0000102 serial_get_poll_events, /* get_poll_events */
Mike McCormack6f011c02001-12-20 00:07:05 +0000103 serial_poll_event, /* poll_event */
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000104 serial_flush, /* flush */
Mike McCormack44b5bf52000-09-07 18:39:51 +0000105 serial_get_info, /* get_file_info */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000106 serial_queue_async /* queue_async */
Mike McCormack44b5bf52000-09-07 18:39:51 +0000107};
108
Mike McCormack568c67e2001-10-08 20:40:57 +0000109static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000110{
111 struct serial *serial;
112 struct termios tios;
113 int fd, flags = 0;
114 char *name;
115
116 if (!(name = mem_alloc( len + 1 ))) return NULL;
117 memcpy( name, nameptr, len );
118 name[len] = 0;
119
120 switch(access & (GENERIC_READ | GENERIC_WRITE))
121 {
122 case GENERIC_READ: flags |= O_RDONLY; break;
123 case GENERIC_WRITE: flags |= O_WRONLY; break;
124 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
125 default: break;
126 }
127
Mike McCormack05b66182001-08-21 17:01:48 +0000128 flags |= O_NONBLOCK;
129
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000130 fd = open( name, flags );
131 free( name );
132 if (fd < 0)
133 {
134 file_set_error();
135 return NULL;
136 }
137
138 /* check its really a serial port */
139 if (tcgetattr(fd,&tios))
140 {
141 file_set_error();
142 close( fd );
143 return NULL;
144 }
145
Mike McCormack568c67e2001-10-08 20:40:57 +0000146 /* set the fd back to blocking if necessary */
147 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
148 if(0>fcntl(fd, F_SETFL, 0))
149 perror("fcntl");
150
Alexandre Julliard863637b2003-01-30 00:26:44 +0000151 if ((serial = alloc_fd_object( &serial_ops, &serial_fd_ops, fd )))
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000152 {
Mike McCormack568c67e2001-10-08 20:40:57 +0000153 serial->attrib = attributes;
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000154 serial->access = access;
155 serial->readinterval = 0;
156 serial->readmult = 0;
157 serial->readconst = 0;
158 serial->writemult = 0;
159 serial->writeconst = 0;
160 serial->eventmask = 0;
161 serial->commerror = 0;
Mike McCormack6f011c02001-12-20 00:07:05 +0000162 init_async_queue(&serial->read_q);
163 init_async_queue(&serial->write_q);
164 init_async_queue(&serial->wait_q);
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000165 }
166 return serial;
167}
168
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000169static void serial_destroy( struct object *obj)
Mike McCormack6f011c02001-12-20 00:07:05 +0000170{
171 struct serial *serial = (struct serial *)obj;
172
173 destroy_async_queue(&serial->read_q);
174 destroy_async_queue(&serial->write_q);
175 destroy_async_queue(&serial->wait_q);
176}
177
Mike McCormack44b5bf52000-09-07 18:39:51 +0000178static void serial_dump( struct object *obj, int verbose )
179{
180 struct serial *serial = (struct serial *)obj;
181 assert( obj->ops == &serial_ops );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000182 fprintf( stderr, "Port fd=%p mask=%x\n", serial->obj.fd_obj, serial->eventmask );
Mike McCormack44b5bf52000-09-07 18:39:51 +0000183}
184
Alexandre Julliard51885742002-05-30 20:12:58 +0000185struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000186{
187 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
188}
189
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000190static int serial_get_poll_events( struct fd *fd )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000191{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000192 struct serial *serial = get_fd_user( fd );
Mike McCormack44b5bf52000-09-07 18:39:51 +0000193 int events = 0;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000194 assert( serial->obj.ops == &serial_ops );
Mike McCormack6f011c02001-12-20 00:07:05 +0000195
196 if(IS_READY(serial->read_q))
197 events |= POLLIN;
198 if(IS_READY(serial->write_q))
199 events |= POLLOUT;
200 if(IS_READY(serial->wait_q))
201 events |= POLLIN;
202
203 /* fprintf(stderr,"poll events are %04x\n",events); */
204
Mike McCormack44b5bf52000-09-07 18:39:51 +0000205 return events;
206}
207
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000208static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000209{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000210 struct serial *serial = get_fd_user( fd );
211 assert( serial->obj.ops == &serial_ops );
Mike McCormack568c67e2001-10-08 20:40:57 +0000212
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000213 if (reply)
Mike McCormackff58be52001-10-04 16:18:15 +0000214 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000215 reply->type = FILE_TYPE_CHAR;
216 reply->attr = 0;
217 reply->access_time = 0;
218 reply->write_time = 0;
219 reply->size_high = 0;
220 reply->size_low = 0;
221 reply->links = 0;
222 reply->index_high = 0;
223 reply->index_low = 0;
224 reply->serial = 0;
Mike McCormackff58be52001-10-04 16:18:15 +0000225 }
Mike McCormack568c67e2001-10-08 20:40:57 +0000226
Martin Wilck88cd32b2002-01-09 20:30:51 +0000227 *flags = 0;
Mike McCormack568c67e2001-10-08 20:40:57 +0000228 if(serial->attrib & FILE_FLAG_OVERLAPPED)
Martin Wilck88cd32b2002-01-09 20:30:51 +0000229 *flags |= FD_FLAG_OVERLAPPED;
230 else if(!((serial->readinterval == MAXDWORD) &&
231 (serial->readmult == 0) && (serial->readconst == 0)) )
232 *flags |= FD_FLAG_TIMEOUT;
Mike McCormack568c67e2001-10-08 20:40:57 +0000233
Martin Wilck88cd32b2002-01-09 20:30:51 +0000234 return FD_TYPE_DEFAULT;
Mike McCormack44b5bf52000-09-07 18:39:51 +0000235}
236
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000237static void serial_poll_event(struct fd *fd, int event)
Mike McCormack1eac1912000-11-13 19:27:21 +0000238{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000239 struct serial *serial = get_fd_user( fd );
Mike McCormack1eac1912000-11-13 19:27:21 +0000240
Mike McCormack6f011c02001-12-20 00:07:05 +0000241 /* fprintf(stderr,"Poll event %02x\n",event); */
242
243 if(IS_READY(serial->read_q) && (POLLIN & event) )
244 async_notify(serial->read_q.head,STATUS_ALERTED);
245
246 if(IS_READY(serial->write_q) && (POLLOUT & event) )
247 async_notify(serial->write_q.head,STATUS_ALERTED);
248
249 if(IS_READY(serial->wait_q) && (POLLIN & event) )
250 async_notify(serial->wait_q.head,STATUS_ALERTED);
251
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000252 set_select_events( &serial->obj, serial_get_poll_events(fd) );
Mike McCormack6f011c02001-12-20 00:07:05 +0000253}
254
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000255static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
Mike McCormack6f011c02001-12-20 00:07:05 +0000256{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000257 struct serial *serial = get_fd_user( fd );
Mike McCormack6f011c02001-12-20 00:07:05 +0000258 struct async_queue *q;
Martin Wilck54ba2722002-04-24 21:29:54 +0000259 struct async *async;
Mike McCormack6f011c02001-12-20 00:07:05 +0000260 int timeout;
261
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000262 assert(serial->obj.ops == &serial_ops);
Mike McCormack1eac1912000-11-13 19:27:21 +0000263
Michael McCormack1c32a462001-03-22 20:09:34 +0000264 switch(type)
Mike McCormack1eac1912000-11-13 19:27:21 +0000265 {
266 case ASYNC_TYPE_READ:
Mike McCormack6f011c02001-12-20 00:07:05 +0000267 q = &serial->read_q;
268 timeout = serial->readconst + serial->readmult*count;
269 break;
270 case ASYNC_TYPE_WAIT:
271 q = &serial->wait_q;
272 timeout = 0;
273 break;
Mike McCormack1eac1912000-11-13 19:27:21 +0000274 case ASYNC_TYPE_WRITE:
Mike McCormack6f011c02001-12-20 00:07:05 +0000275 q = &serial->write_q;
276 timeout = serial->writeconst + serial->writemult*count;
277 break;
278 default:
279 set_error(STATUS_INVALID_PARAMETER);
Martin Wilck54ba2722002-04-24 21:29:54 +0000280 return;
Mike McCormack1eac1912000-11-13 19:27:21 +0000281 }
Mike McCormack6f011c02001-12-20 00:07:05 +0000282
Martin Wilck54ba2722002-04-24 21:29:54 +0000283 async = find_async ( q, current, ptr );
284
285 if ( status == STATUS_PENDING )
Mike McCormack6f011c02001-12-20 00:07:05 +0000286 {
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000287 int events;
Martin Wilckff1f3202002-04-26 18:31:19 +0000288
Martin Wilck54ba2722002-04-24 21:29:54 +0000289 if ( !async )
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000290 async = create_async ( &serial->obj, current, ptr );
Martin Wilck54ba2722002-04-24 21:29:54 +0000291 if ( !async )
292 return;
293
294 async->status = STATUS_PENDING;
Mike McCormack6f011c02001-12-20 00:07:05 +0000295 if(!async->q)
296 {
297 async_add_timeout(async,timeout);
298 async_insert(q, async);
Martin Wilck54ba2722002-04-24 21:29:54 +0000299 }
Martin Wilckff1f3202002-04-26 18:31:19 +0000300
301 /* Check if the new pending request can be served immediately */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000302 events = check_fd_events( fd, serial_get_poll_events( fd ) );
303 if (events)
304 {
Martin Wilckff1f3202002-04-26 18:31:19 +0000305 /* serial_poll_event() calls set_select_events() */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000306 serial_poll_event( fd, events );
307 return;
308 }
Mike McCormack6f011c02001-12-20 00:07:05 +0000309 }
Martin Wilck54ba2722002-04-24 21:29:54 +0000310 else if ( async ) destroy_async ( async );
311 else set_error ( STATUS_INVALID_PARAMETER );
Mike McCormack1eac1912000-11-13 19:27:21 +0000312
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000313 set_select_events ( &serial->obj, serial_get_poll_events( fd ));
Mike McCormack6f011c02001-12-20 00:07:05 +0000314}
Mike McCormack1eac1912000-11-13 19:27:21 +0000315
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000316static int serial_flush( struct fd *fd )
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000317{
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000318 /* MSDN says: If hFile is a handle to a communications device,
319 * the function only flushes the transmit buffer.
320 */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000321 int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000322 if (!ret) file_set_error();
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000323 return ret;
324}
325
Mike McCormack44b5bf52000-09-07 18:39:51 +0000326/* create a serial */
327DECL_HANDLER(create_serial)
328{
329 struct serial *serial;
Mike McCormack44b5bf52000-09-07 18:39:51 +0000330
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000331 reply->handle = 0;
332 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
Mike McCormack44b5bf52000-09-07 18:39:51 +0000333 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000334 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
Mike McCormack44b5bf52000-09-07 18:39:51 +0000335 release_object( serial );
336 }
337}
338
Mike McCormack654fcc72000-09-16 20:55:12 +0000339DECL_HANDLER(get_serial_info)
340{
341 struct serial *serial;
342
343 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
344 {
345 /* timeouts */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000346 reply->readinterval = serial->readinterval;
347 reply->readconst = serial->readconst;
348 reply->readmult = serial->readmult;
349 reply->writeconst = serial->writeconst;
350 reply->writemult = serial->writemult;
Mike McCormack654fcc72000-09-16 20:55:12 +0000351
352 /* event mask */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000353 reply->eventmask = serial->eventmask;
Mike McCormack654fcc72000-09-16 20:55:12 +0000354
355 /* comm port error status */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000356 reply->commerror = serial->commerror;
Mike McCormack654fcc72000-09-16 20:55:12 +0000357
358 release_object( serial );
359 }
360}
361
362DECL_HANDLER(set_serial_info)
363{
364 struct serial *serial;
365
366 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
367 {
368 /* timeouts */
369 if(req->flags & SERIALINFO_SET_TIMEOUTS)
370 {
371 serial->readinterval = req->readinterval;
372 serial->readconst = req->readconst;
373 serial->readmult = req->readmult;
374 serial->writeconst = req->writeconst;
375 serial->writemult = req->writemult;
376 }
377
378 /* event mask */
379 if(req->flags & SERIALINFO_SET_MASK)
380 {
381 serial->eventmask = req->eventmask;
Mike McCormack32521ab2002-03-12 19:19:57 +0000382 if(!serial->eventmask)
383 {
384 while(serial->wait_q.head)
385 {
386 async_notify(serial->wait_q.head, STATUS_SUCCESS);
387 destroy_async(serial->wait_q.head);
388 }
389 }
Mike McCormack654fcc72000-09-16 20:55:12 +0000390 }
391
392 /* comm port error status */
393 if(req->flags & SERIALINFO_SET_ERROR)
394 {
395 serial->commerror = req->commerror;
396 }
397
398 release_object( serial );
399 }
400}