Fixed some bugs in my recently submitted patch series.
[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 <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #ifdef HAVE_SYS_ERRNO_H
33 #include <sys/errno.h>
34 #endif
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <utime.h>
40 #include <termios.h>
41 #include <sys/ioctl.h>
42
43 #include "winerror.h"
44 #include "winbase.h"
45
46 #include "handle.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "async.h"
50
51 static void serial_dump( struct object *obj, int verbose );
52 static int serial_get_fd( struct object *obj );
53 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
54 static int serial_get_poll_events( struct object *obj );
55 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
56 static void destroy_serial(struct object *obj);
57 static void serial_poll_event( struct object *obj, int event );
58
59 struct serial
60 {
61     struct object       obj;
62     unsigned int        access;
63     unsigned int        attrib;
64
65     /* timeout values */
66     unsigned int        readinterval;
67     unsigned int        readconst;
68     unsigned int        readmult;
69     unsigned int        writeconst;
70     unsigned int        writemult;
71
72     unsigned int        eventmask;
73     unsigned int        commerror;
74
75     struct termios      original;
76
77     struct async_queue  read_q;
78     struct async_queue  write_q;
79     struct async_queue  wait_q;
80
81     /* FIXME: add dcb, comm status, handler module, sharing */
82 };
83
84 static const struct object_ops serial_ops =
85 {
86     sizeof(struct serial),        /* size */
87     serial_dump,                  /* dump */
88     default_poll_add_queue,       /* add_queue */
89     default_poll_remove_queue,    /* remove_queue */
90     default_poll_signaled,        /* signaled */
91     no_satisfied,                 /* satisfied */
92     serial_get_poll_events,       /* get_poll_events */
93     serial_poll_event,            /* poll_event */
94     serial_get_fd,                /* get_fd */
95     no_flush,                     /* flush */
96     serial_get_info,              /* get_file_info */
97     serial_queue_async,           /* queue_async */
98     destroy_serial                /* destroy */
99 };
100
101 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
102 {
103     struct serial *serial;
104     struct termios tios;
105     int fd, flags = 0;
106     char *name;
107
108     if (!(name = mem_alloc( len + 1 ))) return NULL;
109     memcpy( name, nameptr, len );
110     name[len] = 0;
111
112     switch(access & (GENERIC_READ | GENERIC_WRITE))
113     {
114     case GENERIC_READ:  flags |= O_RDONLY; break;
115     case GENERIC_WRITE: flags |= O_WRONLY; break;
116     case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
117     default: break;
118     }
119
120     flags |= O_NONBLOCK;
121
122     fd = open( name, flags );
123     free( name );
124     if (fd < 0)
125     {
126         file_set_error();
127         return NULL;
128     }
129
130     /* check its really a serial port */
131     if (tcgetattr(fd,&tios))
132     {
133         file_set_error();
134         close( fd );
135         return NULL;
136     }
137
138     /* set the fd back to blocking if necessary */
139     if( ! (attributes & FILE_FLAG_OVERLAPPED) )
140        if(0>fcntl(fd, F_SETFL, 0))
141            perror("fcntl");
142
143     if ((serial = alloc_object( &serial_ops, fd )))
144     {
145         serial->attrib       = attributes;
146         serial->access       = access;
147         serial->readinterval = 0;
148         serial->readmult     = 0;
149         serial->readconst    = 0;
150         serial->writemult    = 0;
151         serial->writeconst   = 0;
152         serial->eventmask    = 0;
153         serial->commerror    = 0;
154         init_async_queue(&serial->read_q);
155         init_async_queue(&serial->write_q);
156         init_async_queue(&serial->wait_q);
157     }
158     return serial;
159 }
160
161 static void destroy_serial( struct object *obj)
162 {
163     struct serial *serial = (struct serial *)obj;
164
165     destroy_async_queue(&serial->read_q);
166     destroy_async_queue(&serial->write_q);
167     destroy_async_queue(&serial->wait_q);
168 }
169
170 static void serial_dump( struct object *obj, int verbose )
171 {
172     struct serial *serial = (struct serial *)obj;
173     assert( obj->ops == &serial_ops );
174     fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
175 }
176
177 struct serial *get_serial_obj( struct process *process, handle_t handle, unsigned int access )
178 {
179     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
180 }
181
182 static int serial_get_poll_events( struct object *obj )
183 {
184     struct serial *serial = (struct serial *)obj;
185     int events = 0;
186     assert( obj->ops == &serial_ops );
187
188     if(IS_READY(serial->read_q))
189         events |= POLLIN;
190     if(IS_READY(serial->write_q))
191         events |= POLLOUT;
192     if(IS_READY(serial->wait_q))
193         events |= POLLIN;
194
195     /* fprintf(stderr,"poll events are %04x\n",events); */
196
197     return events;
198 }
199
200 static int serial_get_fd( struct object *obj )
201 {
202     struct serial *serial = (struct serial *)obj;
203     assert( obj->ops == &serial_ops );
204     return serial->obj.fd;
205 }
206
207 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
208 {
209     struct serial *serial = (struct serial *) obj;
210     assert( obj->ops == &serial_ops );
211
212     if (reply)
213     {
214         reply->type        = FILE_TYPE_CHAR;
215         reply->attr        = 0;
216         reply->access_time = 0;
217         reply->write_time  = 0;
218         reply->size_high   = 0;
219         reply->size_low    = 0;
220         reply->links       = 0;
221         reply->index_high  = 0;
222         reply->index_low   = 0;
223         reply->serial      = 0;
224     }
225
226     *flags = 0;
227     if(serial->attrib & FILE_FLAG_OVERLAPPED)
228         *flags |= FD_FLAG_OVERLAPPED;
229     else if(!((serial->readinterval == MAXDWORD) &&
230               (serial->readmult == 0) && (serial->readconst == 0)) )
231         *flags |= FD_FLAG_TIMEOUT;
232
233     return FD_TYPE_DEFAULT;
234 }
235
236 static void serial_poll_event(struct object *obj, int event)
237 {
238     struct serial *serial = (struct serial *)obj;
239
240     /* fprintf(stderr,"Poll event %02x\n",event); */
241
242     if(IS_READY(serial->read_q) && (POLLIN & event) )
243         async_notify(serial->read_q.head,STATUS_ALERTED);
244
245     if(IS_READY(serial->write_q) && (POLLOUT & event) )
246         async_notify(serial->write_q.head,STATUS_ALERTED);
247
248     if(IS_READY(serial->wait_q) && (POLLIN & event) )
249         async_notify(serial->wait_q.head,STATUS_ALERTED);
250
251     set_select_events(obj,obj->ops->get_poll_events(obj));
252 }
253
254 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
255 {
256     struct serial *serial = (struct serial *)obj;
257     struct async_queue *q;
258     struct async *async;
259     int timeout;
260
261     assert(obj->ops == &serial_ops);
262
263     switch(type)
264     {
265     case ASYNC_TYPE_READ:
266         q = &serial->read_q;
267         timeout = serial->readconst + serial->readmult*count;
268         break;
269     case ASYNC_TYPE_WAIT:
270         q = &serial->wait_q;
271         timeout = 0;
272         break;
273     case ASYNC_TYPE_WRITE:
274         q = &serial->write_q;
275         timeout = serial->writeconst + serial->writemult*count;
276         break;
277     default:
278         set_error(STATUS_INVALID_PARAMETER);
279         return;
280     }
281
282     async = find_async ( q, current, ptr );
283
284     if ( status == STATUS_PENDING )
285     {
286         struct pollfd pfd;
287
288         if ( !async )
289             async = create_async ( obj, current, ptr );
290         if ( !async )
291             return;
292
293         async->status = STATUS_PENDING;
294         if(!async->q)
295         {
296             async_add_timeout(async,timeout);
297             async_insert(q, async);
298         }
299
300         /* Check if the new pending request can be served immediately */
301         pfd.fd = obj->fd;
302         pfd.events = serial_get_poll_events ( obj );
303         pfd.revents = 0;
304         poll ( &pfd, 1, 0 );
305
306         if ( pfd.revents )
307             /* serial_poll_event() calls set_select_events() */
308             serial_poll_event ( obj, pfd.revents );
309         else
310             set_select_events ( obj, pfd.events );
311         return;
312     }
313     else if ( async ) destroy_async ( async );
314     else set_error ( STATUS_INVALID_PARAMETER );
315
316     set_select_events ( obj, serial_get_poll_events ( obj ));
317 }
318
319 /* create a serial */
320 DECL_HANDLER(create_serial)
321 {
322     struct serial *serial;
323
324     reply->handle = 0;
325     if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
326     {
327         reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
328         release_object( serial );
329     }
330 }
331
332 DECL_HANDLER(get_serial_info)
333 {
334     struct serial *serial;
335
336     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
337     {
338         /* timeouts */
339         reply->readinterval = serial->readinterval;
340         reply->readconst    = serial->readconst;
341         reply->readmult     = serial->readmult;
342         reply->writeconst   = serial->writeconst;
343         reply->writemult    = serial->writemult;
344
345         /* event mask */
346         reply->eventmask    = serial->eventmask;
347
348         /* comm port error status */
349         reply->commerror    = serial->commerror;
350
351         release_object( serial );
352     }
353 }
354
355 DECL_HANDLER(set_serial_info)
356 {
357     struct serial *serial;
358
359     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
360     {
361         /* timeouts */
362         if(req->flags & SERIALINFO_SET_TIMEOUTS)
363         {
364             serial->readinterval = req->readinterval;
365             serial->readconst    = req->readconst;
366             serial->readmult     = req->readmult;
367             serial->writeconst   = req->writeconst;
368             serial->writemult    = req->writemult;
369         }
370
371         /* event mask */
372         if(req->flags & SERIALINFO_SET_MASK)
373         {
374             serial->eventmask = req->eventmask;
375             if(!serial->eventmask)
376             {
377                 while(serial->wait_q.head)
378                 {
379                     async_notify(serial->wait_q.head, STATUS_SUCCESS);
380                     destroy_async(serial->wait_q.head);
381                 }
382             }
383         }
384
385         /* comm port error status */
386         if(req->flags & SERIALINFO_SET_ERROR)
387         {
388             serial->commerror = req->commerror;
389         }
390
391         release_object( serial );
392     }
393 }
394