server: Use the new set_fd_user function in create_serial().
[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 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
48
49 #include "ntstatus.h"
50 #define WIN32_NO_STATUS
51 #include "windef.h"
52 #include "winternl.h"
53
54 #include "file.h"
55 #include "handle.h"
56 #include "thread.h"
57 #include "request.h"
58
59 static void serial_dump( struct object *obj, int verbose );
60 static struct fd *serial_get_fd( struct object *obj );
61 static unsigned int serial_map_access( struct object *obj, unsigned int access );
62 static void serial_destroy(struct object *obj);
63
64 static int serial_get_poll_events( struct fd *fd );
65 static void serial_poll_event( struct fd *fd, int event );
66 static int serial_get_info( struct fd *fd );
67 static int serial_flush( struct fd *fd, struct event **event );
68 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
69 static void serial_cancel_async( struct fd *fd );
70
71 struct serial
72 {
73     struct object       obj;
74     struct fd          *fd;
75     unsigned int        options;
76
77     /* timeout values */
78     unsigned int        readinterval;
79     unsigned int        readconst;
80     unsigned int        readmult;
81     unsigned int        writeconst;
82     unsigned int        writemult;
83
84     unsigned int        eventmask;
85     unsigned int        commerror;
86
87     struct termios      original;
88
89     struct list         read_q;
90     struct list         write_q;
91     struct list         wait_q;
92
93     /* FIXME: add dcb, comm status, handler module, sharing */
94 };
95
96 static const struct object_ops serial_ops =
97 {
98     sizeof(struct serial),        /* size */
99     serial_dump,                  /* dump */
100     default_fd_add_queue,         /* add_queue */
101     default_fd_remove_queue,      /* remove_queue */
102     default_fd_signaled,          /* signaled */
103     no_satisfied,                 /* satisfied */
104     no_signal,                    /* signal */
105     serial_get_fd,                /* get_fd */
106     serial_map_access,            /* map_access */
107     no_lookup_name,               /* lookup_name */
108     no_close_handle,              /* close_handle */
109     serial_destroy                /* destroy */
110 };
111
112 static const struct fd_ops serial_fd_ops =
113 {
114     serial_get_poll_events,       /* get_poll_events */
115     serial_poll_event,            /* poll_event */
116     serial_flush,                 /* flush */
117     serial_get_info,              /* get_file_info */
118     serial_queue_async,           /* queue_async */
119     serial_cancel_async           /* cancel_async */
120 };
121
122 /* check if the given fd is a serial port */
123 int is_serial_fd( struct fd *fd )
124 {
125     struct termios tios;
126
127     return !tcgetattr( get_unix_fd(fd), &tios );
128 }
129
130 /* create a serial object for a given fd */
131 struct object *create_serial( struct fd *fd, unsigned int options )
132 {
133     struct serial *serial;
134     int unix_fd = get_unix_fd( fd );
135
136     /* set the fd back to blocking if necessary */
137     if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
138         fcntl( unix_fd, F_SETFL, 0 );
139
140     if (!(serial = alloc_object( &serial_ops ))) return NULL;
141
142     serial->options      = options;
143     serial->readinterval = 0;
144     serial->readmult     = 0;
145     serial->readconst    = 0;
146     serial->writemult    = 0;
147     serial->writeconst   = 0;
148     serial->eventmask    = 0;
149     serial->commerror    = 0;
150     list_init( &serial->read_q );
151     list_init( &serial->write_q );
152     list_init( &serial->wait_q );
153     serial->fd = (struct fd *)grab_object( fd );
154     set_fd_user( fd, &serial_fd_ops, &serial->obj );
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 unsigned int serial_map_access( struct object *obj, unsigned int access )
165 {
166     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
167     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
168     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
169     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
170     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
171 }
172
173 static void serial_destroy( struct object *obj)
174 {
175     struct serial *serial = (struct serial *)obj;
176
177     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
178     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
179     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
180     if (serial->fd) release_object( serial->fd );
181 }
182
183 static void serial_dump( struct object *obj, int verbose )
184 {
185     struct serial *serial = (struct serial *)obj;
186     assert( obj->ops == &serial_ops );
187     fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
188 }
189
190 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
191 {
192     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
193 }
194
195 static int serial_get_poll_events( struct fd *fd )
196 {
197     struct serial *serial = get_fd_user( fd );
198     int events = 0;
199     assert( serial->obj.ops == &serial_ops );
200
201     if (!list_empty( &serial->read_q ))  events |= POLLIN;
202     if (!list_empty( &serial->write_q )) events |= POLLOUT;
203     if (!list_empty( &serial->wait_q ))  events |= POLLIN;
204
205     /* fprintf(stderr,"poll events are %04x\n",events); */
206
207     return events;
208 }
209
210 static int serial_get_info( struct fd *fd )
211 {
212     int flags = 0;
213     struct serial *serial = get_fd_user( fd );
214     assert( serial->obj.ops == &serial_ops );
215
216     if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
217         flags |= FD_FLAG_OVERLAPPED;
218     else if (!(serial->readinterval == MAXDWORD &&
219                serial->readmult == 0 && serial->readconst == 0))
220         flags |= FD_FLAG_TIMEOUT;
221     if (serial->readinterval == MAXDWORD &&
222         serial->readmult == 0 && serial->readconst == 0)
223         flags |= FD_FLAG_AVAILABLE;
224
225     return flags;
226 }
227
228 static void serial_poll_event(struct fd *fd, int event)
229 {
230     struct serial *serial = get_fd_user( fd );
231
232     /* fprintf(stderr,"Poll event %02x\n",event); */
233
234     if (!list_empty( &serial->read_q ) && (POLLIN & event) )
235         async_terminate_head( &serial->read_q, STATUS_ALERTED );
236
237     if (!list_empty( &serial->write_q ) && (POLLOUT & event) )
238         async_terminate_head( &serial->write_q, STATUS_ALERTED );
239
240     if (!list_empty( &serial->wait_q ) && (POLLIN & event) )
241         async_terminate_head( &serial->wait_q, STATUS_ALERTED );
242
243     set_fd_events( fd, serial_get_poll_events(fd) );
244 }
245
246 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
247                                 int type, int count )
248 {
249     struct serial *serial = get_fd_user( fd );
250     struct list *queue;
251     int timeout;
252     int events;
253
254     assert(serial->obj.ops == &serial_ops);
255
256     switch (type)
257     {
258     case ASYNC_TYPE_READ:
259         queue = &serial->read_q;
260         timeout = serial->readconst + serial->readmult*count;
261         break;
262     case ASYNC_TYPE_WAIT:
263         queue = &serial->wait_q;
264         timeout = 0;
265         break;
266     case ASYNC_TYPE_WRITE:
267         queue = &serial->write_q;
268         timeout = serial->writeconst + serial->writemult*count;
269         break;
270     default:
271         set_error(STATUS_INVALID_PARAMETER);
272         return;
273     }
274
275     if (!create_async( current, &timeout, queue, apc, user, iosb ))
276         return;
277
278     /* Check if the new pending request can be served immediately */
279     events = check_fd_events( fd, serial_get_poll_events( fd ) );
280     if (events)
281     {
282         /* serial_poll_event() calls set_select_events() */
283         serial_poll_event( fd, events );
284         return;
285     }
286
287     set_fd_events( fd, serial_get_poll_events( fd ) );
288 }
289
290 static void serial_cancel_async( struct fd *fd )
291 {
292     struct serial *serial = get_fd_user( fd );
293     assert(serial->obj.ops == &serial_ops);
294
295     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
296     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
297     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
298 }
299
300 static int serial_flush( struct fd *fd, struct event **event )
301 {
302     /* MSDN says: If hFile is a handle to a communications device,
303      * the function only flushes the transmit buffer.
304      */
305     int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
306     if (!ret) file_set_error();
307     return ret;
308 }
309
310 DECL_HANDLER(get_serial_info)
311 {
312     struct serial *serial;
313
314     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
315     {
316         /* timeouts */
317         reply->readinterval = serial->readinterval;
318         reply->readconst    = serial->readconst;
319         reply->readmult     = serial->readmult;
320         reply->writeconst   = serial->writeconst;
321         reply->writemult    = serial->writemult;
322
323         /* event mask */
324         reply->eventmask    = serial->eventmask;
325
326         /* comm port error status */
327         reply->commerror    = serial->commerror;
328
329         release_object( serial );
330     }
331 }
332
333 DECL_HANDLER(set_serial_info)
334 {
335     struct serial *serial;
336
337     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
338     {
339         /* timeouts */
340         if (req->flags & SERIALINFO_SET_TIMEOUTS)
341         {
342             serial->readinterval = req->readinterval;
343             serial->readconst    = req->readconst;
344             serial->readmult     = req->readmult;
345             serial->writeconst   = req->writeconst;
346             serial->writemult    = req->writemult;
347         }
348
349         /* event mask */
350         if (req->flags & SERIALINFO_SET_MASK)
351         {
352             serial->eventmask = req->eventmask;
353             if (!serial->eventmask)
354             {
355                 async_terminate_queue( &serial->wait_q, STATUS_SUCCESS );
356             }
357         }
358
359         /* comm port error status */
360         if (req->flags & SERIALINFO_SET_ERROR)
361         {
362             serial->commerror = req->commerror;
363         }
364
365         release_object( serial );
366     }
367 }