serial.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Server-side serial port communications management
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. * Copyright (C) 2000,2001 Mike McCormack
  6. *
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include "config.h"
  23. #include "wine/port.h"
  24. #include <assert.h>
  25. #include <fcntl.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <sys/time.h>
  31. #include <sys/types.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. #ifdef HAVE_UTIME_H
  35. #include <utime.h>
  36. #endif
  37. #ifdef HAVE_TERMIOS_H
  38. #include <termios.h>
  39. #endif
  40. #ifdef HAVE_SYS_IOCTL_H
  41. #include <sys/ioctl.h>
  42. #endif
  43. #ifdef HAVE_POLL_H
  44. #include <poll.h>
  45. #endif
  46. #include "ntstatus.h"
  47. #define WIN32_NO_STATUS
  48. #include "windef.h"
  49. #include "winternl.h"
  50. #include "winioctl.h"
  51. #include "ddk/ntddser.h"
  52. #include "file.h"
  53. #include "handle.h"
  54. #include "thread.h"
  55. #include "request.h"
  56. static void serial_dump( struct object *obj, int verbose );
  57. static struct fd *serial_get_fd( struct object *obj );
  58. static void serial_destroy(struct object *obj);
  59. static enum server_fd_type serial_get_fd_type( struct fd *fd );
  60. static int serial_ioctl( struct fd *fd, ioctl_code_t code, client_ptr_t in_buf, client_ptr_t out_buf, struct async *async );
  61. static void serial_queue_async( struct fd *fd, struct async *async, int type, int count );
  62. static void serial_reselect_async( struct fd *fd, struct async_queue *queue );
  63. struct serial
  64. {
  65. struct object obj;
  66. struct fd *fd;
  67. struct timeout_user *read_timer;
  68. SERIAL_TIMEOUTS timeouts;
  69. unsigned int eventmask;
  70. unsigned int generation; /* event mask change counter */
  71. unsigned int pending_write : 1;
  72. unsigned int pending_wait : 1;
  73. struct termios original;
  74. /* FIXME: add dcb, comm status, handler module, sharing */
  75. };
  76. static const struct object_ops serial_ops =
  77. {
  78. sizeof(struct serial), /* size */
  79. serial_dump, /* dump */
  80. no_get_type, /* get_type */
  81. add_queue, /* add_queue */
  82. remove_queue, /* remove_queue */
  83. default_fd_signaled, /* signaled */
  84. NULL, /* get_esync_fd */
  85. no_satisfied, /* satisfied */
  86. no_signal, /* signal */
  87. serial_get_fd, /* get_fd */
  88. default_fd_map_access, /* map_access */
  89. default_get_sd, /* get_sd */
  90. default_set_sd, /* set_sd */
  91. no_lookup_name, /* lookup_name */
  92. no_link_name, /* link_name */
  93. NULL, /* unlink_name */
  94. no_open_file, /* open_file */
  95. no_kernel_obj_list, /* get_kernel_obj_list */
  96. no_alloc_handle, /* alloc_handle */
  97. fd_close_handle, /* close_handle */
  98. serial_destroy /* destroy */
  99. };
  100. static const struct fd_ops serial_fd_ops =
  101. {
  102. default_fd_get_poll_events, /* get_poll_events */
  103. default_poll_event, /* poll_event */
  104. serial_get_fd_type, /* get_fd_type */
  105. no_fd_read, /* read */
  106. no_fd_write, /* write */
  107. no_fd_flush, /* flush */
  108. default_fd_get_file_info, /* get_file_info */
  109. no_fd_get_volume_info, /* get_volume_info */
  110. serial_ioctl, /* ioctl */
  111. serial_queue_async, /* queue_async */
  112. serial_reselect_async /* reselect_async */
  113. };
  114. /* check if the given fd is a serial port */
  115. int is_serial_fd( struct fd *fd )
  116. {
  117. struct termios tios;
  118. return !tcgetattr( get_unix_fd(fd), &tios );
  119. }
  120. /* create a serial object for a given fd */
  121. struct object *create_serial( struct fd *fd )
  122. {
  123. struct serial *serial;
  124. if (!(serial = alloc_object( &serial_ops ))) return NULL;
  125. serial->read_timer = NULL;
  126. serial->eventmask = 0;
  127. serial->generation = 0;
  128. serial->pending_write = 0;
  129. serial->pending_wait = 0;
  130. memset( &serial->timeouts, 0, sizeof(serial->timeouts) );
  131. serial->fd = (struct fd *)grab_object( fd );
  132. set_fd_user( fd, &serial_fd_ops, &serial->obj );
  133. return &serial->obj;
  134. }
  135. static struct fd *serial_get_fd( struct object *obj )
  136. {
  137. struct serial *serial = (struct serial *)obj;
  138. return (struct fd *)grab_object( serial->fd );
  139. }
  140. static void serial_destroy( struct object *obj)
  141. {
  142. struct serial *serial = (struct serial *)obj;
  143. if (serial->read_timer) remove_timeout_user( serial->read_timer );
  144. release_object( serial->fd );
  145. }
  146. static void serial_dump( struct object *obj, int verbose )
  147. {
  148. struct serial *serial = (struct serial *)obj;
  149. assert( obj->ops == &serial_ops );
  150. fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
  151. }
  152. static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
  153. {
  154. return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
  155. }
  156. static enum server_fd_type serial_get_fd_type( struct fd *fd )
  157. {
  158. return FD_TYPE_SERIAL;
  159. }
  160. static int serial_ioctl( struct fd *fd, ioctl_code_t code, client_ptr_t in_buf, client_ptr_t out_buf, struct async *async )
  161. {
  162. struct serial *serial = get_fd_user( fd );
  163. switch (code)
  164. {
  165. case IOCTL_SERIAL_GET_TIMEOUTS:
  166. if (get_reply_max_size() < sizeof(serial->timeouts))
  167. {
  168. set_error( STATUS_BUFFER_TOO_SMALL );
  169. return 0;
  170. }
  171. set_reply_data( &serial->timeouts, sizeof(serial->timeouts ));
  172. return 1;
  173. case IOCTL_SERIAL_SET_TIMEOUTS:
  174. if (get_req_data_size() < sizeof(serial->timeouts))
  175. {
  176. set_error( STATUS_BUFFER_TOO_SMALL );
  177. return 0;
  178. }
  179. memcpy( &serial->timeouts, get_req_data(), sizeof(serial->timeouts) );
  180. return 1;
  181. case IOCTL_SERIAL_GET_WAIT_MASK:
  182. if (get_reply_max_size() < sizeof(serial->eventmask))
  183. {
  184. set_error( STATUS_BUFFER_TOO_SMALL );
  185. return 0;
  186. }
  187. set_reply_data( &serial->eventmask, sizeof(serial->eventmask) );
  188. return 1;
  189. case IOCTL_SERIAL_SET_WAIT_MASK:
  190. if (get_req_data_size() < sizeof(serial->eventmask))
  191. {
  192. set_error( STATUS_BUFFER_TOO_SMALL );
  193. return 0;
  194. }
  195. serial->eventmask = *(unsigned int *)get_req_data();
  196. serial->generation++;
  197. fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
  198. return 1;
  199. default:
  200. set_error( STATUS_NOT_SUPPORTED );
  201. return 0;
  202. }
  203. }
  204. static void serial_queue_async( struct fd *fd, struct async *async, int type, int count )
  205. {
  206. struct serial *serial = get_fd_user( fd );
  207. timeout_t timeout = 0;
  208. assert(serial->obj.ops == &serial_ops);
  209. switch (type)
  210. {
  211. case ASYNC_TYPE_READ:
  212. timeout = serial->timeouts.ReadTotalTimeoutConstant +
  213. (timeout_t)serial->timeouts.ReadTotalTimeoutMultiplier * count;
  214. break;
  215. case ASYNC_TYPE_WRITE:
  216. timeout = serial->timeouts.WriteTotalTimeoutConstant +
  217. (timeout_t)serial->timeouts.WriteTotalTimeoutMultiplier * count;
  218. break;
  219. }
  220. fd_queue_async( fd, async, type );
  221. if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
  222. set_error( STATUS_PENDING );
  223. }
  224. static void serial_read_timeout( void *arg )
  225. {
  226. struct serial *serial = arg;
  227. serial->read_timer = NULL;
  228. fd_async_wake_up( serial->fd, ASYNC_TYPE_READ, STATUS_TIMEOUT );
  229. }
  230. static void serial_reselect_async( struct fd *fd, struct async_queue *queue )
  231. {
  232. struct serial *serial = get_fd_user( fd );
  233. if (serial->read_timer)
  234. {
  235. if (!(default_fd_get_poll_events( fd ) & POLLIN))
  236. {
  237. remove_timeout_user( serial->read_timer );
  238. serial->read_timer = NULL;
  239. }
  240. }
  241. else if (serial->timeouts.ReadIntervalTimeout && (default_fd_get_poll_events( fd ) & POLLIN))
  242. {
  243. serial->read_timer = add_timeout_user( (timeout_t)serial->timeouts.ReadIntervalTimeout * -10000,
  244. serial_read_timeout, serial );
  245. }
  246. default_fd_reselect_async( fd, queue );
  247. }
  248. DECL_HANDLER(get_serial_info)
  249. {
  250. struct serial *serial;
  251. if ((serial = get_serial_obj( current->process, req->handle, 0 )))
  252. {
  253. if (req->flags & SERIALINFO_PENDING_WAIT)
  254. {
  255. if (serial->pending_wait)
  256. {
  257. release_object( serial );
  258. set_error( STATUS_INVALID_PARAMETER );
  259. return;
  260. }
  261. serial->pending_wait = 1;
  262. }
  263. /* event mask */
  264. reply->eventmask = serial->eventmask;
  265. reply->cookie = serial->generation;
  266. /* pending write */
  267. reply->pending_write = serial->pending_write;
  268. if (req->flags & SERIALINFO_PENDING_WRITE)
  269. serial->pending_write = 0;
  270. release_object( serial );
  271. }
  272. }
  273. DECL_HANDLER(set_serial_info)
  274. {
  275. struct serial *serial;
  276. if ((serial = get_serial_obj( current->process, req->handle, 0 )))
  277. {
  278. if (req->flags & SERIALINFO_PENDING_WAIT)
  279. {
  280. if (!serial->pending_wait)
  281. {
  282. release_object( serial );
  283. set_error( STATUS_INVALID_PARAMETER );
  284. return;
  285. }
  286. serial->pending_wait = 0;
  287. }
  288. /* pending write */
  289. if (req->flags & SERIALINFO_PENDING_WRITE)
  290. serial->pending_write = 1;
  291. release_object( serial );
  292. }
  293. }