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;
100 fd
= open( name
, flags
);
108 /* check its really a serial port */
109 if (tcgetattr(fd
,&tios
))
116 if ((serial
= alloc_object( &serial_ops
, fd
)))
118 serial
->access
= access
;
119 serial
->readinterval
= 0;
120 serial
->readmult
= 0;
121 serial
->readconst
= 0;
122 serial
->writemult
= 0;
123 serial
->writeconst
= 0;
124 serial
->eventmask
= 0;
125 serial
->commerror
= 0;
130 static void serial_dump( struct object
*obj
, int verbose
)
132 struct serial
*serial
= (struct serial
*)obj
;
133 assert( obj
->ops
== &serial_ops
);
134 fprintf( stderr
, "Port fd=%d mask=%x\n", serial
->obj
.fd
, serial
->eventmask
);
137 struct serial
*get_serial_obj( struct process
*process
, handle_t handle
, unsigned int access
)
139 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
142 static int serial_get_poll_events( struct object
*obj
)
144 struct serial
*serial
= (struct serial
*)obj
;
146 assert( obj
->ops
== &serial_ops
);
147 if (serial
->access
& GENERIC_READ
) events
|= POLLIN
;
148 if (serial
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
152 static int serial_get_fd( struct object
*obj
)
154 struct serial
*serial
= (struct serial
*)obj
;
155 assert( obj
->ops
== &serial_ops
);
156 return serial
->obj
.fd
;
159 static int serial_get_info( struct object
*obj
, struct get_file_info_request
*req
)
161 assert( obj
->ops
== &serial_ops
);
162 req
->type
= FILE_TYPE_CHAR
;
164 req
->access_time
= 0;
175 /* these function calculates the timeout for an async operation
177 int get_serial_async_timeout(struct object
*obj
, int type
, int count
)
179 struct serial
*serial
= (struct serial
*)obj
;
181 if(obj
->ops
!= &serial_ops
)
186 case ASYNC_TYPE_READ
:
187 return serial
->readconst
+ serial
->readmult
*count
;
188 case ASYNC_TYPE_WRITE
:
189 return serial
->writeconst
+ serial
->writemult
*count
;
195 /* create a serial */
196 DECL_HANDLER(create_serial
)
198 struct serial
*serial
;
201 if ((serial
= create_serial( get_req_data(req
), get_req_data_size(req
), req
->access
)))
203 req
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
204 release_object( serial
);
208 DECL_HANDLER(get_serial_info
)
210 struct serial
*serial
;
212 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
215 req
->readinterval
= serial
->readinterval
;
216 req
->readconst
= serial
->readconst
;
217 req
->readmult
= serial
->readmult
;
218 req
->writeconst
= serial
->writeconst
;
219 req
->writemult
= serial
->writemult
;
222 req
->eventmask
= serial
->eventmask
;
224 /* comm port error status */
225 req
->commerror
= serial
->commerror
;
227 release_object( serial
);
231 DECL_HANDLER(set_serial_info
)
233 struct serial
*serial
;
235 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
238 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
240 serial
->readinterval
= req
->readinterval
;
241 serial
->readconst
= req
->readconst
;
242 serial
->readmult
= req
->readmult
;
243 serial
->writeconst
= req
->writeconst
;
244 serial
->writemult
= req
->writemult
;
248 if(req
->flags
& SERIALINFO_SET_MASK
)
250 serial
->eventmask
= req
->eventmask
;
253 /* comm port error status */
254 if(req
->flags
& SERIALINFO_SET_ERROR
)
256 serial
->commerror
= req
->commerror
;
259 release_object( serial
);