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_request
*req
);
41 static int serial_get_poll_events( struct object
*obj
);
49 unsigned int readinterval
;
50 unsigned int readconst
;
51 unsigned int readmult
;
52 unsigned int writeconst
;
53 unsigned int writemult
;
55 unsigned int eventmask
;
56 unsigned int commerror
;
58 struct termios original
;
60 /* FIXME: add dcb, comm status, handler module, sharing */
63 static const struct object_ops serial_ops
=
65 sizeof(struct serial
), /* size */
66 serial_dump
, /* dump */
67 default_poll_add_queue
, /* add_queue */
68 default_poll_remove_queue
, /* remove_queue */
69 default_poll_signaled
, /* signaled */
70 no_satisfied
, /* satisfied */
71 serial_get_poll_events
, /* get_poll_events */
72 default_poll_event
, /* poll_event */
73 serial_get_fd
, /* get_fd */
75 serial_get_info
, /* get_file_info */
76 no_destroy
/* destroy */
79 /* SERIAL PORT functions */
81 static struct serial
*create_serial( const char *nameptr
, size_t len
, unsigned int access
)
83 struct serial
*serial
;
88 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
89 memcpy( name
, nameptr
, len
);
92 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
94 case GENERIC_READ
: flags
|= O_RDONLY
; break;
95 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
96 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
102 fd
= open( name
, flags
);
110 /* check its really a serial port */
111 if (tcgetattr(fd
,&tios
))
118 if ((serial
= alloc_object( &serial_ops
, fd
)))
120 serial
->access
= access
;
121 serial
->readinterval
= 0;
122 serial
->readmult
= 0;
123 serial
->readconst
= 0;
124 serial
->writemult
= 0;
125 serial
->writeconst
= 0;
126 serial
->eventmask
= 0;
127 serial
->commerror
= 0;
132 static void serial_dump( struct object
*obj
, int verbose
)
134 struct serial
*serial
= (struct serial
*)obj
;
135 assert( obj
->ops
== &serial_ops
);
136 fprintf( stderr
, "Port fd=%d mask=%x\n", serial
->obj
.fd
, serial
->eventmask
);
139 struct serial
*get_serial_obj( struct process
*process
, handle_t handle
, unsigned int access
)
141 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
144 static int serial_get_poll_events( struct object
*obj
)
146 struct serial
*serial
= (struct serial
*)obj
;
148 assert( obj
->ops
== &serial_ops
);
149 if (serial
->access
& GENERIC_READ
) events
|= POLLIN
;
150 if (serial
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
154 static int serial_get_fd( struct object
*obj
)
156 struct serial
*serial
= (struct serial
*)obj
;
157 assert( obj
->ops
== &serial_ops
);
158 return serial
->obj
.fd
;
161 static int serial_get_info( struct object
*obj
, struct get_file_info_request
*req
)
163 assert( obj
->ops
== &serial_ops
);
164 req
->type
= FILE_TYPE_CHAR
;
166 req
->access_time
= 0;
177 /* these function calculates the timeout for an async operation
179 int get_serial_async_timeout(struct object
*obj
, int type
, int count
)
181 struct serial
*serial
= (struct serial
*)obj
;
183 if(obj
->ops
!= &serial_ops
)
188 case ASYNC_TYPE_READ
:
189 return serial
->readconst
+ serial
->readmult
*count
;
190 case ASYNC_TYPE_WRITE
:
191 return serial
->writeconst
+ serial
->writemult
*count
;
197 /* create a serial */
198 DECL_HANDLER(create_serial
)
200 struct serial
*serial
;
203 if ((serial
= create_serial( get_req_data(req
), get_req_data_size(req
), req
->access
)))
205 req
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
206 release_object( serial
);
210 DECL_HANDLER(get_serial_info
)
212 struct serial
*serial
;
214 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
217 req
->readinterval
= serial
->readinterval
;
218 req
->readconst
= serial
->readconst
;
219 req
->readmult
= serial
->readmult
;
220 req
->writeconst
= serial
->writeconst
;
221 req
->writemult
= serial
->writemult
;
224 req
->eventmask
= serial
->eventmask
;
226 /* comm port error status */
227 req
->commerror
= serial
->commerror
;
229 release_object( serial
);
233 DECL_HANDLER(set_serial_info
)
235 struct serial
*serial
;
237 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
240 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
242 serial
->readinterval
= req
->readinterval
;
243 serial
->readconst
= req
->readconst
;
244 serial
->readmult
= req
->readmult
;
245 serial
->writeconst
= req
->writeconst
;
246 serial
->writemult
= req
->writemult
;
250 if(req
->flags
& SERIALINFO_SET_MASK
)
252 serial
->eventmask
= req
->eventmask
;
255 /* comm port error status */
256 if(req
->flags
& SERIALINFO_SET_ERROR
)
258 serial
->commerror
= req
->commerror
;
261 release_object( serial
);