- got rid of include/async.h
[wine] / server / serial.c
1 /*
2  * Server-side serial port communications management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  * Copyright (C) 2000,2001 Mike McCormack
6  *
7  *
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
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include <unistd.h>
36 #ifdef HAVE_UTIME_H
37 #include <utime.h>
38 #endif
39 #ifdef HAVE_TERMIOS_H
40 #include <termios.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winreg.h"
50 #include "winternl.h"
51
52 #include "file.h"
53 #include "handle.h"
54 #include "thread.h"
55 #include "request.h"
56
57 static void serial_dump( struct object *obj, int verbose );
58 static struct fd *serial_get_fd( struct object *obj );
59 static void serial_destroy(struct object *obj);
60
61 static int serial_get_poll_events( struct fd *fd );
62 static void serial_poll_event( struct fd *fd, int event );
63 static int serial_get_info( struct fd *fd );
64 static int serial_flush( struct fd *fd, struct event **event );
65 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
66 static void serial_cancel_async( struct fd *fd );
67
68 struct serial
69 {
70     struct object       obj;
71     struct fd          *fd;
72     unsigned int        options;
73
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
86     struct async       *read_q;
87     struct async       *write_q;
88     struct async       *wait_q;
89
90     /* FIXME: add dcb, comm status, handler module, sharing */
91 };
92
93 static const struct object_ops serial_ops =
94 {
95     sizeof(struct serial),        /* size */
96     serial_dump,                  /* dump */
97     default_fd_add_queue,         /* add_queue */
98     default_fd_remove_queue,      /* remove_queue */
99     default_fd_signaled,          /* signaled */
100     no_satisfied,                 /* satisfied */
101     serial_get_fd,                /* get_fd */
102     serial_destroy                /* destroy */
103 };
104
105 static const struct fd_ops serial_fd_ops =
106 {
107     serial_get_poll_events,       /* get_poll_events */
108     serial_poll_event,            /* poll_event */
109     serial_flush,                 /* flush */
110     serial_get_info,              /* get_file_info */
111     serial_queue_async,           /* queue_async */
112     serial_cancel_async           /* cancel_async */
113 };
114
115 /* check if the given fd is a serial port */
116 int is_serial_fd( struct fd *fd )
117 {
118     struct termios tios;
119
120     return !tcgetattr( get_unix_fd(fd), &tios );
121 }
122
123 /* create a serial object for a given fd */
124 struct object *create_serial( struct fd *fd, unsigned int options )
125 {
126     struct serial *serial;
127     int unix_fd;
128
129     if ((unix_fd = dup( get_unix_fd(fd) )) == -1) return NULL;
130
131     /* set the fd back to blocking if necessary */
132     if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
133         fcntl( unix_fd, F_SETFL, 0 );
134
135     if (!(serial = alloc_object( &serial_ops )))
136     {
137         close( unix_fd );
138         return NULL;
139     }
140     serial->options      = options;
141     serial->readinterval = 0;
142     serial->readmult     = 0;
143     serial->readconst    = 0;
144     serial->writemult    = 0;
145     serial->writeconst   = 0;
146     serial->eventmask    = 0;
147     serial->commerror    = 0;
148     serial->read_q = serial->write_q = serial->wait_q = NULL;
149     if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, unix_fd, &serial->obj )))
150     {
151         release_object( serial );
152         return NULL;
153     }
154     return &serial->obj;
155 }
156
157 static struct fd *serial_get_fd( struct object *obj )
158 {
159     struct serial *serial = (struct serial *)obj;
160     return (struct fd *)grab_object( serial->fd );
161 }
162
163 static void serial_destroy( struct object *obj)
164 {
165     struct serial *serial = (struct serial *)obj;
166
167     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
168     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
169     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
170     if (serial->fd) release_object( serial->fd );
171 }
172
173 static void serial_dump( struct object *obj, int verbose )
174 {
175     struct serial *serial = (struct serial *)obj;
176     assert( obj->ops == &serial_ops );
177     fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
178 }
179
180 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
181 {
182     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
183 }
184
185 static int serial_get_poll_events( struct fd *fd )
186 {
187     struct serial *serial = get_fd_user( fd );
188     int events = 0;
189     assert( serial->obj.ops == &serial_ops );
190
191     if (serial->read_q)  events |= POLLIN;
192     if (serial->write_q) events |= POLLOUT;
193     if (serial->wait_q)  events |= POLLIN;
194
195     /* fprintf(stderr,"poll events are %04x\n",events); */
196
197     return events;
198 }
199
200 static int serial_get_info( struct fd *fd )
201 {
202     int flags = 0;
203     struct serial *serial = get_fd_user( fd );
204     assert( serial->obj.ops == &serial_ops );
205
206     if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
207         flags |= FD_FLAG_OVERLAPPED;
208     else if (!(serial->readinterval == MAXDWORD &&
209                serial->readmult == 0 && serial->readconst == 0))
210         flags |= FD_FLAG_TIMEOUT;
211     if (serial->readinterval == MAXDWORD &&
212         serial->readmult == 0 && serial->readconst == 0)
213         flags |= FD_FLAG_AVAILABLE;
214
215     return flags;
216 }
217
218 static void serial_poll_event(struct fd *fd, int event)
219 {
220     struct serial *serial = get_fd_user( fd );
221
222     /* fprintf(stderr,"Poll event %02x\n",event); */
223
224     if (serial->read_q && (POLLIN & event) )
225         async_terminate( serial->read_q, STATUS_ALERTED );
226
227     if (serial->write_q && (POLLOUT & event) )
228         async_terminate( serial->write_q, STATUS_ALERTED );
229
230     if (serial->wait_q && (POLLIN & event) )
231         async_terminate( serial->wait_q, STATUS_ALERTED );
232
233     set_fd_events( fd, serial_get_poll_events(fd) );
234 }
235
236 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
237                                 int type, int count )
238 {
239     struct serial *serial = get_fd_user( fd );
240     struct async **head;
241     int timeout;
242     int events;
243
244     assert(serial->obj.ops == &serial_ops);
245
246     switch (type)
247     {
248     case ASYNC_TYPE_READ:
249         head = &serial->read_q;
250         timeout = serial->readconst + serial->readmult*count;
251         break;
252     case ASYNC_TYPE_WAIT:
253         head = &serial->wait_q;
254         timeout = 0;
255         break;
256     case ASYNC_TYPE_WRITE:
257         head = &serial->write_q;
258         timeout = serial->writeconst + serial->writemult*count;
259         break;
260     default:
261         set_error(STATUS_INVALID_PARAMETER);
262         return;
263     }
264
265     if (!create_async( fd, current, timeout, head, apc, user, iosb ))
266         return;
267
268     /* Check if the new pending request can be served immediately */
269     events = check_fd_events( fd, serial_get_poll_events( fd ) );
270     if (events)
271     {
272         /* serial_poll_event() calls set_select_events() */
273         serial_poll_event( fd, events );
274         return;
275     }
276
277     set_fd_events( fd, serial_get_poll_events( fd ) );
278 }
279
280 static void serial_cancel_async( struct fd *fd )
281 {
282     struct serial *serial = get_fd_user( fd );
283     assert(serial->obj.ops == &serial_ops);
284
285     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
286     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
287     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
288 }
289
290 static int serial_flush( struct fd *fd, struct event **event )
291 {
292     /* MSDN says: If hFile is a handle to a communications device,
293      * the function only flushes the transmit buffer.
294      */
295     int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
296     if (!ret) file_set_error();
297     return ret;
298 }
299
300 DECL_HANDLER(get_serial_info)
301 {
302     struct serial *serial;
303
304     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
305     {
306         /* timeouts */
307         reply->readinterval = serial->readinterval;
308         reply->readconst    = serial->readconst;
309         reply->readmult     = serial->readmult;
310         reply->writeconst   = serial->writeconst;
311         reply->writemult    = serial->writemult;
312
313         /* event mask */
314         reply->eventmask    = serial->eventmask;
315
316         /* comm port error status */
317         reply->commerror    = serial->commerror;
318
319         release_object( serial );
320     }
321 }
322
323 DECL_HANDLER(set_serial_info)
324 {
325     struct serial *serial;
326
327     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
328     {
329         /* timeouts */
330         if (req->flags & SERIALINFO_SET_TIMEOUTS)
331         {
332             serial->readinterval = req->readinterval;
333             serial->readconst    = req->readconst;
334             serial->readmult     = req->readmult;
335             serial->writeconst   = req->writeconst;
336             serial->writemult    = req->writemult;
337         }
338
339         /* event mask */
340         if (req->flags & SERIALINFO_SET_MASK)
341         {
342             serial->eventmask = req->eventmask;
343             if (!serial->eventmask)
344             {
345                 async_terminate_queue( &serial->wait_q, STATUS_SUCCESS );
346             }
347         }
348
349         /* comm port error status */
350         if (req->flags & SERIALINFO_SET_ERROR)
351         {
352             serial->commerror = req->commerror;
353         }
354
355         release_object( serial );
356     }
357 }