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 /* create a serial */
186 DECL_HANDLER(create_serial)
188 struct serial *serial;
191 if ((serial = create_serial( get_req_data(req), get_req_data_size(req), req->access )))
193 req->handle = alloc_handle( current->process, serial, req->access, req->inherit );
194 release_object( serial );
198 DECL_HANDLER(get_serial_info)
200 struct serial *serial;
202 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
205 req->readinterval = serial->readinterval;
206 req->readconst = serial->readconst;
207 req->readmult = serial->readmult;
208 req->writeconst = serial->writeconst;
209 req->writemult = serial->writemult;
212 req->eventmask = serial->eventmask;
214 /* comm port error status */
215 req->commerror = serial->commerror;
217 release_object( serial );
221 DECL_HANDLER(set_serial_info)
223 struct serial *serial;
225 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
228 if(req->flags & SERIALINFO_SET_TIMEOUTS)
230 serial->readinterval = req->readinterval;
231 serial->readconst = req->readconst;
232 serial->readmult = req->readmult;
233 serial->writeconst = req->writeconst;
234 serial->writemult = req->writemult;
238 if(req->flags & SERIALINFO_SET_MASK)
240 serial->eventmask = req->eventmask;
243 /* comm port error status */
244 if(req->flags & SERIALINFO_SET_ERROR)
246 serial->commerror = req->commerror;
249 release_object( serial );