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 void serial_destroy( struct object
*obj
);
41 static int serial_get_read_fd( struct object
*obj
);
42 static int serial_get_write_fd( struct object
*obj
);
43 static int serial_get_info( struct object
*obj
, struct get_file_info_request
*req
);
44 static int serial_get_poll_events( struct object
*obj
);
49 char name
[16]; /* eg. /dev/ttyS1 */
53 unsigned int readinterval
;
54 unsigned int readconst
;
55 unsigned int readmult
;
56 unsigned int writeconst
;
57 unsigned int writemult
;
59 unsigned int eventmask
;
60 unsigned int commerror
;
62 struct termios original
;
64 /* FIXME: add dcb, comm status, handler module, sharing */
67 static const struct object_ops serial_ops
=
69 sizeof(struct serial
), /* size */
70 serial_dump
, /* dump */
71 default_poll_add_queue
, /* add_queue */
72 default_poll_remove_queue
, /* remove_queue */
73 default_poll_signaled
, /* signaled */
74 no_satisfied
, /* satisfied */
75 serial_get_poll_events
, /* get_poll_events */
76 default_poll_event
, /* poll_event */
77 serial_get_read_fd
, /* get_read_fd */
78 serial_get_write_fd
, /* get_write_fd */
80 serial_get_info
, /* get_file_info */
81 serial_destroy
/* destroy */
84 /* SERIAL PORT functions */
86 static void serial_dump( struct object
*obj
, int verbose
)
88 struct serial
*serial
= (struct serial
*)obj
;
89 assert( obj
->ops
== &serial_ops
);
91 fprintf( stderr
, "Port fd=%d name='%s' mask=%x\n",
92 serial
->obj
.fd
, serial
->name
,serial
->eventmask
);
95 /* same as file_destroy, but don't delete comm ports */
96 static void serial_destroy( struct object
*obj
)
98 assert( obj
->ops
== &serial_ops
);
101 struct serial
*get_serial_obj( struct process
*process
, int handle
, unsigned int access
)
103 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
106 static int serial_get_poll_events( struct object
*obj
)
108 struct serial
*serial
= (struct serial
*)obj
;
110 assert( obj
->ops
== &serial_ops
);
111 if (serial
->access
& GENERIC_READ
) events
|= POLLIN
;
112 if (serial
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
116 static int serial_get_read_fd( struct object
*obj
)
118 struct serial
*serial
= (struct serial
*)obj
;
119 assert( obj
->ops
== &serial_ops
);
120 return dup( serial
->obj
.fd
);
123 static int serial_get_write_fd( struct object
*obj
)
125 struct serial
*serial
= (struct serial
*)obj
;
126 assert( obj
->ops
== &serial_ops
);
127 return dup( serial
->obj
.fd
);
130 static int serial_get_info( struct object
*obj
, struct get_file_info_request
*req
)
132 assert( obj
->ops
== &serial_ops
);
133 req
->type
= FILE_TYPE_CHAR
;
135 req
->access_time
= 0;
146 /* create a serial */
147 DECL_HANDLER(create_serial
)
149 struct serial
*serial
;
156 switch(req
->access
& (GENERIC_READ
| GENERIC_WRITE
))
158 case GENERIC_READ
: flags
|= O_RDONLY
; break;
159 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
160 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
164 fd
= open( req
->name
, flags
);
171 /* check its really a serial port */
172 if(0>tcgetattr(fd
,&tios
))
179 serial
= alloc_object( &serial_ops
, fd
);
182 strncpy(serial
->name
,req
->name
,sizeof serial
->name
);
183 serial
->name
[sizeof(serial
->name
)-1] = 0;
185 serial
->access
= req
->access
;
186 serial
->readinterval
= 0;
187 serial
->readmult
= 0;
188 serial
->readconst
= 0;
189 serial
->writemult
= 0;
190 serial
->writeconst
= 0;
191 serial
->eventmask
= 0;
192 serial
->commerror
= 0;
194 req
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
195 release_object( serial
);
199 DECL_HANDLER(get_serial_info
)
201 struct serial
*serial
;
203 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
206 req
->readinterval
= serial
->readinterval
;
207 req
->readconst
= serial
->readconst
;
208 req
->readmult
= serial
->readmult
;
209 req
->writeconst
= serial
->writeconst
;
210 req
->writemult
= serial
->writemult
;
213 req
->eventmask
= serial
->eventmask
;
215 /* comm port error status */
216 req
->commerror
= serial
->commerror
;
218 release_object( serial
);
222 DECL_HANDLER(set_serial_info
)
224 struct serial
*serial
;
226 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
229 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
231 serial
->readinterval
= req
->readinterval
;
232 serial
->readconst
= req
->readconst
;
233 serial
->readmult
= req
->readmult
;
234 serial
->writeconst
= req
->writeconst
;
235 serial
->writemult
= req
->writemult
;
239 if(req
->flags
& SERIALINFO_SET_MASK
)
241 serial
->eventmask
= req
->eventmask
;
244 /* comm port error status */
245 if(req
->flags
& SERIALINFO_SET_ERROR
)
247 serial
->commerror
= req
->commerror
;
250 release_object( serial
);