Make the standard create_file request handle serial ports too, and
[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 #include "async.h"
57
58 static void serial_dump( struct object *obj, int verbose );
59 static struct fd *serial_get_fd( struct object *obj );
60 static void serial_destroy(struct object *obj);
61
62 static int serial_get_poll_events( struct fd *fd );
63 static void serial_poll_event( struct fd *fd, int event );
64 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
65 static int serial_flush( struct fd *fd, struct event **event );
66 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count);
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_queue  read_q;
87     struct async_queue  write_q;
88     struct async_queue  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 };
113
114 /* check if the given fd is a serial port */
115 int 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 */
123 struct object *create_serial( struct fd *fd, unsigned int options )
124 {
125     struct serial *serial;
126     int unix_fd;
127
128     if ((unix_fd = dup( get_unix_fd(fd) )) == -1) return NULL;
129
130     /* set the fd back to blocking if necessary */
131     if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
132         fcntl( unix_fd, F_SETFL, 0 );
133
134     if (!(serial = alloc_object( &serial_ops )))
135     {
136         close( unix_fd );
137         return NULL;
138     }
139     serial->options      = options;
140     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);
150     if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, unix_fd, &serial->obj )))
151     {
152         release_object( serial );
153         return NULL;
154     }
155     return &serial->obj;
156 }
157
158 static 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
164 static void serial_destroy( struct object *obj)
165 {
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);
171     if (serial->fd) release_object( serial->fd );
172 }
173
174 static void serial_dump( struct object *obj, int verbose )
175 {
176     struct serial *serial = (struct serial *)obj;
177     assert( obj->ops == &serial_ops );
178     fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
179 }
180
181 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
182 {
183     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
184 }
185
186 static int serial_get_poll_events( struct fd *fd )
187 {
188     struct serial *serial = get_fd_user( fd );
189     int events = 0;
190     assert( serial->obj.ops == &serial_ops );
191
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
201     return events;
202 }
203
204 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
205 {
206     struct serial *serial = get_fd_user( fd );
207     assert( serial->obj.ops == &serial_ops );
208
209     if (reply)
210     {
211         reply->type        = FILE_TYPE_CHAR;
212         reply->attr        = 0;
213         reply->access_time = 0;
214         reply->write_time  = 0;
215         reply->size_high   = 0;
216         reply->size_low    = 0;
217         reply->links       = 0;
218         reply->index_high  = 0;
219         reply->index_low   = 0;
220         reply->serial      = 0;
221     }
222
223     *flags = 0;
224     if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
225         *flags |= FD_FLAG_OVERLAPPED;
226     else if(!((serial->readinterval == MAXDWORD) &&
227               (serial->readmult == 0) && (serial->readconst == 0)) )
228         *flags |= FD_FLAG_TIMEOUT;
229
230     return FD_TYPE_DEFAULT;
231 }
232
233 static void serial_poll_event(struct fd *fd, int event)
234 {
235     struct serial *serial = get_fd_user( fd );
236
237     /* fprintf(stderr,"Poll event %02x\n",event); */
238
239     if(IS_READY(serial->read_q) && (POLLIN & event) )
240         async_notify(serial->read_q.head,STATUS_ALERTED);
241
242     if(IS_READY(serial->write_q) && (POLLOUT & event) )
243         async_notify(serial->write_q.head,STATUS_ALERTED);
244
245     if(IS_READY(serial->wait_q) && (POLLIN & event) )
246         async_notify(serial->wait_q.head,STATUS_ALERTED);
247
248     set_fd_events( fd, serial_get_poll_events(fd) );
249 }
250
251 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
252 {
253     struct serial *serial = get_fd_user( fd );
254     struct async_queue *q;
255     struct async *async;
256     int timeout;
257
258     assert(serial->obj.ops == &serial_ops);
259
260     switch(type)
261     {
262     case ASYNC_TYPE_READ:
263         q = &serial->read_q;
264         timeout = serial->readconst + serial->readmult*count;
265         break;
266     case ASYNC_TYPE_WAIT:
267         q = &serial->wait_q;
268         timeout = 0;
269         break;
270     case ASYNC_TYPE_WRITE:
271         q = &serial->write_q;
272         timeout = serial->writeconst + serial->writemult*count;
273         break;
274     default:
275         set_error(STATUS_INVALID_PARAMETER);
276         return;
277     }
278
279     async = find_async ( q, current, ptr );
280
281     if ( status == STATUS_PENDING )
282     {
283         int events;
284
285         if ( !async )
286             async = create_async ( &serial->obj, current, ptr );
287         if ( !async )
288             return;
289
290         async->status = STATUS_PENDING;
291         if(!async->q)
292         {
293             async_add_timeout(async,timeout);
294             async_insert(q, async);
295         }
296
297         /* Check if the new pending request can be served immediately */
298         events = check_fd_events( fd, serial_get_poll_events( fd ) );
299         if (events)
300         {
301             /* serial_poll_event() calls set_select_events() */
302             serial_poll_event( fd, events );
303             return;
304         }
305     }
306     else if ( async ) destroy_async ( async );
307     else set_error ( STATUS_INVALID_PARAMETER );
308
309     set_fd_events ( fd, serial_get_poll_events( fd ));
310 }
311
312 static int serial_flush( struct fd *fd, struct event **event )
313 {
314     /* MSDN says: If hFile is a handle to a communications device,
315      * the function only flushes the transmit buffer.
316      */
317     int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
318     if (!ret) file_set_error();
319     return ret;
320 }
321
322 DECL_HANDLER(get_serial_info)
323 {
324     struct serial *serial;
325
326     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
327     {
328         /* timeouts */
329         reply->readinterval = serial->readinterval;
330         reply->readconst    = serial->readconst;
331         reply->readmult     = serial->readmult;
332         reply->writeconst   = serial->writeconst;
333         reply->writemult    = serial->writemult;
334
335         /* event mask */
336         reply->eventmask    = serial->eventmask;
337
338         /* comm port error status */
339         reply->commerror    = serial->commerror;
340
341         release_object( serial );
342     }
343 }
344
345 DECL_HANDLER(set_serial_info)
346 {
347     struct serial *serial;
348
349     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
350     {
351         /* timeouts */
352         if(req->flags & SERIALINFO_SET_TIMEOUTS)
353         {
354             serial->readinterval = req->readinterval;
355             serial->readconst    = req->readconst;
356             serial->readmult     = req->readmult;
357             serial->writeconst   = req->writeconst;
358             serial->writemult    = req->writemult;
359         }
360
361         /* event mask */
362         if(req->flags & SERIALINFO_SET_MASK)
363         {
364             serial->eventmask = req->eventmask;
365             if(!serial->eventmask)
366             {
367                 while(serial->wait_q.head)
368                 {
369                     async_notify(serial->wait_q.head, STATUS_SUCCESS);
370                     destroy_async(serial->wait_q.head);
371                 }
372             }
373         }
374
375         /* comm port error status */
376         if(req->flags & SERIALINFO_SET_ERROR)
377         {
378             serial->commerror = req->commerror;
379         }
380
381         release_object( serial );
382     }
383 }