2 * Server-side serial port communications management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2000 Mike McCormack
8 * Add async read, write and WaitCommEvent handling.
20 #ifdef HAVE_SYS_ERRNO_H
21 #include <sys/errno.h>
25 #include <sys/types.h>
30 #include <sys/ioctl.h>
39 static void serial_dump( struct object
*obj
, int verbose
);
40 static int serial_get_fd( struct object
*obj
);
41 static int serial_get_info( struct object
*obj
, struct get_file_info_request
*req
);
42 static int serial_get_poll_events( struct object
*obj
);
50 unsigned int readinterval
;
51 unsigned int readconst
;
52 unsigned int readmult
;
53 unsigned int writeconst
;
54 unsigned int writemult
;
56 unsigned int eventmask
;
57 unsigned int commerror
;
59 struct termios original
;
61 /* FIXME: add dcb, comm status, handler module, sharing */
64 static const struct object_ops serial_ops
=
66 sizeof(struct serial
), /* size */
67 serial_dump
, /* dump */
68 default_poll_add_queue
, /* add_queue */
69 default_poll_remove_queue
, /* remove_queue */
70 default_poll_signaled
, /* signaled */
71 no_satisfied
, /* satisfied */
72 serial_get_poll_events
, /* get_poll_events */
73 default_poll_event
, /* poll_event */
74 serial_get_fd
, /* get_fd */
76 serial_get_info
, /* get_file_info */
77 no_destroy
/* destroy */
80 /* SERIAL PORT functions */
82 static struct serial
*create_serial( const char *nameptr
, size_t len
, unsigned int access
)
84 struct serial
*serial
;
89 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
90 memcpy( name
, nameptr
, len
);
93 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
95 case GENERIC_READ
: flags
|= O_RDONLY
; break;
96 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
97 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
101 fd
= open( name
, flags
);
109 /* check its really a serial port */
110 if (tcgetattr(fd
,&tios
))
117 if ((serial
= alloc_object( &serial_ops
, fd
)))
119 serial
->access
= access
;
120 serial
->readinterval
= 0;
121 serial
->readmult
= 0;
122 serial
->readconst
= 0;
123 serial
->writemult
= 0;
124 serial
->writeconst
= 0;
125 serial
->eventmask
= 0;
126 serial
->commerror
= 0;
131 static void serial_dump( struct object
*obj
, int verbose
)
133 struct serial
*serial
= (struct serial
*)obj
;
134 assert( obj
->ops
== &serial_ops
);
135 fprintf( stderr
, "Port fd=%d mask=%x\n", serial
->obj
.fd
, serial
->eventmask
);
138 struct serial
*get_serial_obj( struct process
*process
, handle_t handle
, unsigned int access
)
140 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
143 static int serial_get_poll_events( struct object
*obj
)
145 struct serial
*serial
= (struct serial
*)obj
;
147 assert( obj
->ops
== &serial_ops
);
148 if (serial
->access
& GENERIC_READ
) events
|= POLLIN
;
149 if (serial
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
153 static int serial_get_fd( struct object
*obj
)
155 struct serial
*serial
= (struct serial
*)obj
;
156 assert( obj
->ops
== &serial_ops
);
157 return serial
->obj
.fd
;
160 static int serial_get_info( struct object
*obj
, struct get_file_info_request
*req
)
162 assert( obj
->ops
== &serial_ops
);
163 req
->type
= FILE_TYPE_CHAR
;
165 req
->access_time
= 0;
176 /* these functions are for interaction with asynchronous i/o objects */
177 int serial_async_setup(struct object
*obj
, struct async
*ov
)
179 struct serial
*serial
= (struct serial
*)obj
;
182 if(obj
->ops
!= &serial_ops
)
185 switch(async_type(ov
))
187 case ASYNC_TYPE_READ
:
188 timeout
= serial
->readconst
+ serial
->readmult
*async_count(ov
);
189 async_add_timeout(ov
, timeout
);
190 async_set_eventmask(ov
,EV_RXCHAR
);
192 case ASYNC_TYPE_WRITE
:
193 timeout
= serial
->writeconst
+ serial
->writemult
*async_count(ov
);
194 async_add_timeout(ov
, timeout
);
195 async_set_eventmask(ov
,EV_TXEMPTY
);
197 case ASYNC_TYPE_WAIT
:
198 async_set_eventmask(ov
,serial
->eventmask
);
205 int serial_async_get_poll_events( struct async
*ov
)
209 switch(async_type(ov
))
211 case ASYNC_TYPE_READ
:
214 case ASYNC_TYPE_WRITE
:
217 case ASYNC_TYPE_WAIT
:
219 * FIXME: here is the spot to implement other WaitCommEvent flags
221 mask
= async_get_eventmask(ov
);
231 /* receive a select event, and output a windows event */
232 int serial_async_poll_event(struct object
*obj
, int event
)
237 * FIXME: here is the spot to implement other WaitCommEvent flags
247 /* create a serial */
248 DECL_HANDLER(create_serial
)
250 struct serial
*serial
;
253 if ((serial
= create_serial( get_req_data(req
), get_req_data_size(req
), req
->access
)))
255 req
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
256 release_object( serial
);
260 DECL_HANDLER(get_serial_info
)
262 struct serial
*serial
;
264 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
267 req
->readinterval
= serial
->readinterval
;
268 req
->readconst
= serial
->readconst
;
269 req
->readmult
= serial
->readmult
;
270 req
->writeconst
= serial
->writeconst
;
271 req
->writemult
= serial
->writemult
;
274 req
->eventmask
= serial
->eventmask
;
276 /* comm port error status */
277 req
->commerror
= serial
->commerror
;
279 release_object( serial
);
283 DECL_HANDLER(set_serial_info
)
285 struct serial
*serial
;
287 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
290 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
292 serial
->readinterval
= req
->readinterval
;
293 serial
->readconst
= req
->readconst
;
294 serial
->readmult
= req
->readmult
;
295 serial
->writeconst
= req
->writeconst
;
296 serial
->writemult
= req
->writemult
;
300 if(req
->flags
& SERIALINFO_SET_MASK
)
302 serial
->eventmask
= req
->eventmask
;
305 /* comm port error status */
306 if(req
->flags
& SERIALINFO_SET_ERROR
)
308 serial
->commerror
= req
->commerror
;
311 release_object( serial
);