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>
24 #include <sys/types.h>
29 #include <sys/ioctl.h>
38 static void serial_dump( struct object
*obj
, int verbose
);
39 static int serial_get_fd( struct object
*obj
);
40 static int serial_get_info( struct object
*obj
, struct get_file_info_reply
*reply
);
41 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
, int attributes
)
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;
103 fd
= open( name
, flags
);
111 /* check its really a serial port */
112 if (tcgetattr(fd
,&tios
))
119 /* set the fd back to blocking if necessary */
120 if( ! (attributes
& FILE_FLAG_OVERLAPPED
) )
121 if(0>fcntl(fd
, F_SETFL
, 0))
124 if ((serial
= alloc_object( &serial_ops
, fd
)))
126 serial
->attrib
= attributes
;
127 serial
->access
= access
;
128 serial
->readinterval
= 0;
129 serial
->readmult
= 0;
130 serial
->readconst
= 0;
131 serial
->writemult
= 0;
132 serial
->writeconst
= 0;
133 serial
->eventmask
= 0;
134 serial
->commerror
= 0;
139 static void serial_dump( struct object
*obj
, int verbose
)
141 struct serial
*serial
= (struct serial
*)obj
;
142 assert( obj
->ops
== &serial_ops
);
143 fprintf( stderr
, "Port fd=%d mask=%x\n", serial
->obj
.fd
, serial
->eventmask
);
146 struct serial
*get_serial_obj( struct process
*process
, handle_t handle
, unsigned int access
)
148 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
151 static int serial_get_poll_events( struct object
*obj
)
153 struct serial
*serial
= (struct serial
*)obj
;
155 assert( obj
->ops
== &serial_ops
);
156 if (serial
->access
& GENERIC_READ
) events
|= POLLIN
;
157 if (serial
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
161 static int serial_get_fd( struct object
*obj
)
163 struct serial
*serial
= (struct serial
*)obj
;
164 assert( obj
->ops
== &serial_ops
);
165 return serial
->obj
.fd
;
168 static int serial_get_info( struct object
*obj
, struct get_file_info_reply
*reply
)
170 struct serial
*serial
= (struct serial
*) obj
;
171 assert( obj
->ops
== &serial_ops
);
175 reply
->type
= FILE_TYPE_CHAR
;
177 reply
->access_time
= 0;
178 reply
->write_time
= 0;
179 reply
->size_high
= 0;
182 reply
->index_high
= 0;
183 reply
->index_low
= 0;
187 if(serial
->attrib
& FILE_FLAG_OVERLAPPED
)
188 return FD_TYPE_OVERLAPPED
;
190 return FD_TYPE_TIMEOUT
;
193 /* these function calculates the timeout for an async operation
195 int get_serial_async_timeout(struct object
*obj
, int type
, int count
)
197 struct serial
*serial
= (struct serial
*)obj
;
199 if(obj
->ops
!= &serial_ops
)
204 case ASYNC_TYPE_READ
:
205 return serial
->readconst
+ serial
->readmult
*count
;
206 case ASYNC_TYPE_WRITE
:
207 return serial
->writeconst
+ serial
->writemult
*count
;
213 /* create a serial */
214 DECL_HANDLER(create_serial
)
216 struct serial
*serial
;
219 if ((serial
= create_serial( get_req_data(), get_req_data_size(), req
->access
, req
->attributes
)))
221 reply
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
222 release_object( serial
);
226 DECL_HANDLER(get_serial_info
)
228 struct serial
*serial
;
230 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
233 reply
->readinterval
= serial
->readinterval
;
234 reply
->readconst
= serial
->readconst
;
235 reply
->readmult
= serial
->readmult
;
236 reply
->writeconst
= serial
->writeconst
;
237 reply
->writemult
= serial
->writemult
;
240 reply
->eventmask
= serial
->eventmask
;
242 /* comm port error status */
243 reply
->commerror
= serial
->commerror
;
245 release_object( serial
);
249 DECL_HANDLER(set_serial_info
)
251 struct serial
*serial
;
253 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
256 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
258 serial
->readinterval
= req
->readinterval
;
259 serial
->readconst
= req
->readconst
;
260 serial
->readmult
= req
->readmult
;
261 serial
->writeconst
= req
->writeconst
;
262 serial
->writemult
= req
->writemult
;
266 if(req
->flags
& SERIALINFO_SET_MASK
)
268 serial
->eventmask
= req
->eventmask
;
271 /* comm port error status */
272 if(req
->flags
& SERIALINFO_SET_ERROR
)
274 serial
->commerror
= req
->commerror
;
277 release_object( serial
);