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_read_fd( struct object
*obj
);
41 static int serial_get_write_fd( struct object
*obj
);
42 static int serial_get_info( struct object
*obj
, struct get_file_info_request
*req
);
43 static int serial_get_poll_events( struct object
*obj
);
51 unsigned int readinterval
;
52 unsigned int readconst
;
53 unsigned int readmult
;
54 unsigned int writeconst
;
55 unsigned int writemult
;
57 unsigned int eventmask
;
58 unsigned int commerror
;
60 struct termios original
;
62 /* FIXME: add dcb, comm status, handler module, sharing */
65 static const struct object_ops serial_ops
=
67 sizeof(struct serial
), /* size */
68 serial_dump
, /* dump */
69 default_poll_add_queue
, /* add_queue */
70 default_poll_remove_queue
, /* remove_queue */
71 default_poll_signaled
, /* signaled */
72 no_satisfied
, /* satisfied */
73 serial_get_poll_events
, /* get_poll_events */
74 default_poll_event
, /* poll_event */
75 serial_get_read_fd
, /* get_read_fd */
76 serial_get_write_fd
, /* get_write_fd */
78 serial_get_info
, /* get_file_info */
79 no_destroy
/* destroy */
82 /* SERIAL PORT functions */
84 static struct serial
*create_serial( const char *nameptr
, size_t len
, unsigned int access
)
86 struct serial
*serial
;
91 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
92 memcpy( name
, nameptr
, len
);
95 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
97 case GENERIC_READ
: flags
|= O_RDONLY
; break;
98 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
99 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
103 fd
= open( name
, flags
);
111 /* check its really a serial port */
112 if (tcgetattr(fd
,&tios
))
119 if ((serial
= alloc_object( &serial_ops
, fd
)))
121 serial
->access
= access
;
122 serial
->readinterval
= 0;
123 serial
->readmult
= 0;
124 serial
->readconst
= 0;
125 serial
->writemult
= 0;
126 serial
->writeconst
= 0;
127 serial
->eventmask
= 0;
128 serial
->commerror
= 0;
133 static void serial_dump( struct object
*obj
, int verbose
)
135 struct serial
*serial
= (struct serial
*)obj
;
136 assert( obj
->ops
== &serial_ops
);
137 fprintf( stderr
, "Port fd=%d mask=%x\n", serial
->obj
.fd
, serial
->eventmask
);
140 struct serial
*get_serial_obj( struct process
*process
, int handle
, unsigned int access
)
142 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
145 static int serial_get_poll_events( struct object
*obj
)
147 struct serial
*serial
= (struct serial
*)obj
;
149 assert( obj
->ops
== &serial_ops
);
150 if (serial
->access
& GENERIC_READ
) events
|= POLLIN
;
151 if (serial
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
155 static int serial_get_read_fd( struct object
*obj
)
157 struct serial
*serial
= (struct serial
*)obj
;
158 assert( obj
->ops
== &serial_ops
);
159 return dup( serial
->obj
.fd
);
162 static int serial_get_write_fd( struct object
*obj
)
164 struct serial
*serial
= (struct serial
*)obj
;
165 assert( obj
->ops
== &serial_ops
);
166 return dup( serial
->obj
.fd
);
169 static int serial_get_info( struct object
*obj
, struct get_file_info_request
*req
)
171 assert( obj
->ops
== &serial_ops
);
172 req
->type
= FILE_TYPE_CHAR
;
174 req
->access_time
= 0;
185 /* these functions are for interaction with asynchronous i/o objects */
186 int serial_async_setup(struct object
*obj
, struct async
*ov
)
188 struct serial
*serial
= (struct serial
*)obj
;
191 if(obj
->ops
!= &serial_ops
)
194 switch(async_type(ov
))
196 case ASYNC_TYPE_READ
:
197 timeout
= serial
->readconst
+ serial
->readmult
*async_count(ov
);
198 async_add_timeout(ov
, timeout
);
199 async_set_eventmask(ov
,EV_RXCHAR
);
201 case ASYNC_TYPE_WRITE
:
202 timeout
= serial
->writeconst
+ serial
->writemult
*async_count(ov
);
203 async_add_timeout(ov
, timeout
);
204 async_set_eventmask(ov
,EV_TXEMPTY
);
206 case ASYNC_TYPE_WAIT
:
207 async_set_eventmask(ov
,serial
->eventmask
);
214 int serial_async_get_poll_events( struct async
*ov
)
218 switch(async_type(ov
))
220 case ASYNC_TYPE_READ
:
223 case ASYNC_TYPE_WRITE
:
226 case ASYNC_TYPE_WAIT
:
228 * FIXME: here is the spot to implement other WaitCommEvent flags
230 mask
= async_get_eventmask(ov
);
240 /* receive a select event, and output a windows event */
241 int serial_async_poll_event(struct object
*obj
, int event
)
246 * FIXME: here is the spot to implement other WaitCommEvent flags
256 /* create a serial */
257 DECL_HANDLER(create_serial
)
259 struct serial
*serial
;
262 if ((serial
= create_serial( get_req_data(req
), get_req_data_size(req
), req
->access
)))
264 req
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
265 release_object( serial
);
269 DECL_HANDLER(get_serial_info
)
271 struct serial
*serial
;
273 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
276 req
->readinterval
= serial
->readinterval
;
277 req
->readconst
= serial
->readconst
;
278 req
->readmult
= serial
->readmult
;
279 req
->writeconst
= serial
->writeconst
;
280 req
->writemult
= serial
->writemult
;
283 req
->eventmask
= serial
->eventmask
;
285 /* comm port error status */
286 req
->commerror
= serial
->commerror
;
288 release_object( serial
);
292 DECL_HANDLER(set_serial_info
)
294 struct serial
*serial
;
296 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
299 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
301 serial
->readinterval
= req
->readinterval
;
302 serial
->readconst
= req
->readconst
;
303 serial
->readmult
= req
->readmult
;
304 serial
->writeconst
= req
->writeconst
;
305 serial
->writemult
= req
->writemult
;
309 if(req
->flags
& SERIALINFO_SET_MASK
)
311 serial
->eventmask
= req
->eventmask
;
314 /* comm port error status */
315 if(req
->flags
& SERIALINFO_SET_ERROR
)
317 serial
->commerror
= req
->commerror
;
320 release_object( serial
);