blob: 5dd55d76fc51834c4b77e3d82aa074d729778b83 [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>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000028#include <stdarg.h>
Mike McCormack44b5bf52000-09-07 18:39:51 +000029#include <stdio.h>
30#include <string.h>
31#include <stdlib.h>
Mike McCormack44b5bf52000-09-07 18:39:51 +000032#include <sys/time.h>
33#include <sys/types.h>
34#include <time.h>
35#include <unistd.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000036#ifdef HAVE_UTIME_H
Mike McCormack44b5bf52000-09-07 18:39:51 +000037#include <utime.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000038#endif
39#ifdef HAVE_TERMIOS_H
Mike McCormack44b5bf52000-09-07 18:39:51 +000040#include <termios.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000041#endif
42#ifdef HAVE_SYS_IOCTL_H
Mike McCormack44b5bf52000-09-07 18:39:51 +000043#include <sys/ioctl.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000044#endif
Mike McCormack44b5bf52000-09-07 18:39:51 +000045
46#include "winerror.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000047#include "windef.h"
Mike McCormack44b5bf52000-09-07 18:39:51 +000048#include "winbase.h"
Alexandre Julliardadc06102004-03-18 04:08:48 +000049#include "winreg.h"
50#include "winternl.h"
Mike McCormack44b5bf52000-09-07 18:39:51 +000051
Alexandre Julliard863637b2003-01-30 00:26:44 +000052#include "file.h"
Mike McCormack44b5bf52000-09-07 18:39:51 +000053#include "handle.h"
54#include "thread.h"
55#include "request.h"
Mike McCormack6f011c02001-12-20 00:07:05 +000056#include "async.h"
Mike McCormack44b5bf52000-09-07 18:39:51 +000057
58static void serial_dump( struct object *obj, int verbose );
Alexandre Julliarde66207e2003-02-19 00:33:32 +000059static struct fd *serial_get_fd( struct object *obj );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000060static void serial_destroy(struct object *obj);
61
62static int serial_get_poll_events( struct fd *fd );
63static void serial_poll_event( struct fd *fd, int event );
Alexandre Julliard18c08d32004-04-08 19:09:04 +000064static int serial_get_info( struct fd *fd, int *flags );
Mike McCormackef8b9462003-05-15 04:22:45 +000065static int serial_flush( struct fd *fd, struct event **event );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000066static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count);
Mike McCormack44b5bf52000-09-07 18:39:51 +000067
68struct serial
69{
70 struct object obj;
Alexandre Julliarde66207e2003-02-19 00:33:32 +000071 struct fd *fd;
Alexandre Julliardadc06102004-03-18 04:08:48 +000072 unsigned int options;
Mike McCormack44b5bf52000-09-07 18:39:51 +000073
74 /* timeout values */
75 unsigned int readinterval;
76 unsigned int readconst;
77 unsigned int readmult;
78 unsigned int writeconst;
79 unsigned int writemult;
80
81 unsigned int eventmask;
82 unsigned int commerror;
83
84 struct termios original;
85
Mike McCormack6f011c02001-12-20 00:07:05 +000086 struct async_queue read_q;
87 struct async_queue write_q;
88 struct async_queue wait_q;
89
Mike McCormack44b5bf52000-09-07 18:39:51 +000090 /* FIXME: add dcb, comm status, handler module, sharing */
91};
92
93static const struct object_ops serial_ops =
94{
95 sizeof(struct serial), /* size */
96 serial_dump, /* dump */
Alexandre Julliard863637b2003-01-30 00:26:44 +000097 default_fd_add_queue, /* add_queue */
98 default_fd_remove_queue, /* remove_queue */
99 default_fd_signaled, /* signaled */
Mike McCormack44b5bf52000-09-07 18:39:51 +0000100 no_satisfied, /* satisfied */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000101 serial_get_fd, /* get_fd */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000102 serial_destroy /* destroy */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000103};
104
105static const struct fd_ops serial_fd_ops =
106{
Mike McCormack44b5bf52000-09-07 18:39:51 +0000107 serial_get_poll_events, /* get_poll_events */
Mike McCormack6f011c02001-12-20 00:07:05 +0000108 serial_poll_event, /* poll_event */
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000109 serial_flush, /* flush */
Mike McCormack44b5bf52000-09-07 18:39:51 +0000110 serial_get_info, /* get_file_info */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000111 serial_queue_async /* queue_async */
Mike McCormack44b5bf52000-09-07 18:39:51 +0000112};
113
Alexandre Julliardadc06102004-03-18 04:08:48 +0000114/* check if the given fd is a serial port */
115int is_serial_fd( struct fd *fd )
116{
117 struct termios tios;
118
119 return !tcgetattr( get_unix_fd(fd), &tios );
120}
121
122/* create a serial object for a given fd */
123struct object *create_serial( struct fd *fd, unsigned int options )
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000124{
125 struct serial *serial;
Alexandre Julliardadc06102004-03-18 04:08:48 +0000126 int unix_fd;
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000127
Alexandre Julliardadc06102004-03-18 04:08:48 +0000128 if ((unix_fd = dup( get_unix_fd(fd) )) == -1) return NULL;
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000129
Mike McCormack568c67e2001-10-08 20:40:57 +0000130 /* set the fd back to blocking if necessary */
Alexandre Julliardadc06102004-03-18 04:08:48 +0000131 if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
132 fcntl( unix_fd, F_SETFL, 0 );
Mike McCormack568c67e2001-10-08 20:40:57 +0000133
Alexandre Julliard580da242003-03-12 22:38:14 +0000134 if (!(serial = alloc_object( &serial_ops )))
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000135 {
Alexandre Julliardadc06102004-03-18 04:08:48 +0000136 close( unix_fd );
Alexandre Julliard580da242003-03-12 22:38:14 +0000137 return NULL;
138 }
Alexandre Julliardadc06102004-03-18 04:08:48 +0000139 serial->options = options;
Alexandre Julliard580da242003-03-12 22:38:14 +0000140 serial->readinterval = 0;
141 serial->readmult = 0;
142 serial->readconst = 0;
143 serial->writemult = 0;
144 serial->writeconst = 0;
145 serial->eventmask = 0;
146 serial->commerror = 0;
147 init_async_queue(&serial->read_q);
148 init_async_queue(&serial->write_q);
149 init_async_queue(&serial->wait_q);
Alexandre Julliardadc06102004-03-18 04:08:48 +0000150 if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, unix_fd, &serial->obj )))
Alexandre Julliard580da242003-03-12 22:38:14 +0000151 {
152 release_object( serial );
153 return NULL;
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000154 }
Alexandre Julliardadc06102004-03-18 04:08:48 +0000155 return &serial->obj;
Alexandre Julliard57f05e12000-10-15 00:40:25 +0000156}
157
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000158static struct fd *serial_get_fd( struct object *obj )
159{
160 struct serial *serial = (struct serial *)obj;
161 return (struct fd *)grab_object( serial->fd );
162}
163
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000164static void serial_destroy( struct object *obj)
Mike McCormack6f011c02001-12-20 00:07:05 +0000165{
166 struct serial *serial = (struct serial *)obj;
167
168 destroy_async_queue(&serial->read_q);
169 destroy_async_queue(&serial->write_q);
170 destroy_async_queue(&serial->wait_q);
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000171 if (serial->fd) release_object( serial->fd );
Mike McCormack6f011c02001-12-20 00:07:05 +0000172}
173
Mike McCormack44b5bf52000-09-07 18:39:51 +0000174static void serial_dump( struct object *obj, int verbose )
175{
176 struct serial *serial = (struct serial *)obj;
177 assert( obj->ops == &serial_ops );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000178 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
Mike McCormack44b5bf52000-09-07 18:39:51 +0000179}
180
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000181static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000182{
183 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
184}
185
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000186static int serial_get_poll_events( struct fd *fd )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000187{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000188 struct serial *serial = get_fd_user( fd );
Mike McCormack44b5bf52000-09-07 18:39:51 +0000189 int events = 0;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000190 assert( serial->obj.ops == &serial_ops );
Mike McCormack6f011c02001-12-20 00:07:05 +0000191
192 if(IS_READY(serial->read_q))
193 events |= POLLIN;
194 if(IS_READY(serial->write_q))
195 events |= POLLOUT;
196 if(IS_READY(serial->wait_q))
197 events |= POLLIN;
198
199 /* fprintf(stderr,"poll events are %04x\n",events); */
200
Mike McCormack44b5bf52000-09-07 18:39:51 +0000201 return events;
202}
203
Alexandre Julliard18c08d32004-04-08 19:09:04 +0000204static int serial_get_info( struct fd *fd, int *flags )
Mike McCormack44b5bf52000-09-07 18:39:51 +0000205{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000206 struct serial *serial = get_fd_user( fd );
207 assert( serial->obj.ops == &serial_ops );
Mike McCormack568c67e2001-10-08 20:40:57 +0000208
Martin Wilck88cd32b2002-01-09 20:30:51 +0000209 *flags = 0;
Alexandre Julliardadc06102004-03-18 04:08:48 +0000210 if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
Martin Wilck88cd32b2002-01-09 20:30:51 +0000211 *flags |= FD_FLAG_OVERLAPPED;
212 else if(!((serial->readinterval == MAXDWORD) &&
213 (serial->readmult == 0) && (serial->readconst == 0)) )
214 *flags |= FD_FLAG_TIMEOUT;
Mike McCormack568c67e2001-10-08 20:40:57 +0000215
Martin Wilck88cd32b2002-01-09 20:30:51 +0000216 return FD_TYPE_DEFAULT;
Mike McCormack44b5bf52000-09-07 18:39:51 +0000217}
218
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000219static void serial_poll_event(struct fd *fd, int event)
Mike McCormack1eac1912000-11-13 19:27:21 +0000220{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000221 struct serial *serial = get_fd_user( fd );
Mike McCormack1eac1912000-11-13 19:27:21 +0000222
Mike McCormack6f011c02001-12-20 00:07:05 +0000223 /* fprintf(stderr,"Poll event %02x\n",event); */
224
225 if(IS_READY(serial->read_q) && (POLLIN & event) )
226 async_notify(serial->read_q.head,STATUS_ALERTED);
227
228 if(IS_READY(serial->write_q) && (POLLOUT & event) )
229 async_notify(serial->write_q.head,STATUS_ALERTED);
230
231 if(IS_READY(serial->wait_q) && (POLLIN & event) )
232 async_notify(serial->wait_q.head,STATUS_ALERTED);
233
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000234 set_fd_events( fd, serial_get_poll_events(fd) );
Mike McCormack6f011c02001-12-20 00:07:05 +0000235}
236
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000237static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
Mike McCormack6f011c02001-12-20 00:07:05 +0000238{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000239 struct serial *serial = get_fd_user( fd );
Mike McCormack6f011c02001-12-20 00:07:05 +0000240 struct async_queue *q;
Martin Wilck54ba2722002-04-24 21:29:54 +0000241 struct async *async;
Mike McCormack6f011c02001-12-20 00:07:05 +0000242 int timeout;
243
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000244 assert(serial->obj.ops == &serial_ops);
Mike McCormack1eac1912000-11-13 19:27:21 +0000245
Michael McCormack1c32a462001-03-22 20:09:34 +0000246 switch(type)
Mike McCormack1eac1912000-11-13 19:27:21 +0000247 {
248 case ASYNC_TYPE_READ:
Mike McCormack6f011c02001-12-20 00:07:05 +0000249 q = &serial->read_q;
250 timeout = serial->readconst + serial->readmult*count;
251 break;
252 case ASYNC_TYPE_WAIT:
253 q = &serial->wait_q;
254 timeout = 0;
255 break;
Mike McCormack1eac1912000-11-13 19:27:21 +0000256 case ASYNC_TYPE_WRITE:
Mike McCormack6f011c02001-12-20 00:07:05 +0000257 q = &serial->write_q;
258 timeout = serial->writeconst + serial->writemult*count;
259 break;
260 default:
261 set_error(STATUS_INVALID_PARAMETER);
Martin Wilck54ba2722002-04-24 21:29:54 +0000262 return;
Mike McCormack1eac1912000-11-13 19:27:21 +0000263 }
Mike McCormack6f011c02001-12-20 00:07:05 +0000264
Martin Wilck54ba2722002-04-24 21:29:54 +0000265 async = find_async ( q, current, ptr );
266
267 if ( status == STATUS_PENDING )
Mike McCormack6f011c02001-12-20 00:07:05 +0000268 {
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000269 int events;
Martin Wilckff1f3202002-04-26 18:31:19 +0000270
Martin Wilck54ba2722002-04-24 21:29:54 +0000271 if ( !async )
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000272 async = create_async ( &serial->obj, current, ptr );
Martin Wilck54ba2722002-04-24 21:29:54 +0000273 if ( !async )
274 return;
275
276 async->status = STATUS_PENDING;
Mike McCormack6f011c02001-12-20 00:07:05 +0000277 if(!async->q)
278 {
279 async_add_timeout(async,timeout);
280 async_insert(q, async);
Martin Wilck54ba2722002-04-24 21:29:54 +0000281 }
Martin Wilckff1f3202002-04-26 18:31:19 +0000282
283 /* Check if the new pending request can be served immediately */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000284 events = check_fd_events( fd, serial_get_poll_events( fd ) );
285 if (events)
286 {
Martin Wilckff1f3202002-04-26 18:31:19 +0000287 /* serial_poll_event() calls set_select_events() */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000288 serial_poll_event( fd, events );
289 return;
290 }
Mike McCormack6f011c02001-12-20 00:07:05 +0000291 }
Martin Wilck54ba2722002-04-24 21:29:54 +0000292 else if ( async ) destroy_async ( async );
293 else set_error ( STATUS_INVALID_PARAMETER );
Mike McCormack1eac1912000-11-13 19:27:21 +0000294
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000295 set_fd_events ( fd, serial_get_poll_events( fd ));
Mike McCormack6f011c02001-12-20 00:07:05 +0000296}
Mike McCormack1eac1912000-11-13 19:27:21 +0000297
Mike McCormackef8b9462003-05-15 04:22:45 +0000298static int serial_flush( struct fd *fd, struct event **event )
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000299{
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000300 /* MSDN says: If hFile is a handle to a communications device,
301 * the function only flushes the transmit buffer.
302 */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000303 int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000304 if (!ret) file_set_error();
Marcus Meissner1e54c1f2002-07-16 01:20:38 +0000305 return ret;
306}
307
Mike McCormack654fcc72000-09-16 20:55:12 +0000308DECL_HANDLER(get_serial_info)
309{
310 struct serial *serial;
311
312 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
313 {
314 /* timeouts */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000315 reply->readinterval = serial->readinterval;
316 reply->readconst = serial->readconst;
317 reply->readmult = serial->readmult;
318 reply->writeconst = serial->writeconst;
319 reply->writemult = serial->writemult;
Mike McCormack654fcc72000-09-16 20:55:12 +0000320
321 /* event mask */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000322 reply->eventmask = serial->eventmask;
Mike McCormack654fcc72000-09-16 20:55:12 +0000323
324 /* comm port error status */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000325 reply->commerror = serial->commerror;
Mike McCormack654fcc72000-09-16 20:55:12 +0000326
327 release_object( serial );
328 }
329}
330
331DECL_HANDLER(set_serial_info)
332{
333 struct serial *serial;
334
335 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
336 {
337 /* timeouts */
338 if(req->flags & SERIALINFO_SET_TIMEOUTS)
339 {
340 serial->readinterval = req->readinterval;
341 serial->readconst = req->readconst;
342 serial->readmult = req->readmult;
343 serial->writeconst = req->writeconst;
344 serial->writemult = req->writemult;
345 }
346
347 /* event mask */
348 if(req->flags & SERIALINFO_SET_MASK)
349 {
350 serial->eventmask = req->eventmask;
Mike McCormack32521ab2002-03-12 19:19:57 +0000351 if(!serial->eventmask)
352 {
353 while(serial->wait_q.head)
354 {
355 async_notify(serial->wait_q.head, STATUS_SUCCESS);
356 destroy_async(serial->wait_q.head);
357 }
358 }
Mike McCormack654fcc72000-09-16 20:55:12 +0000359 }
360
361 /* comm port error status */
362 if(req->flags & SERIALINFO_SET_ERROR)
363 {
364 serial->commerror = req->commerror;
365 }
366
367 release_object( serial );
368 }
369}