serial.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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, 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. &file_type, /* type */
  80. serial_dump, /* dump */
  81. add_queue, /* add_queue */
  82. remove_queue, /* remove_queue */
  83. default_fd_signaled, /* signaled */
  84. no_satisfied, /* satisfied */
  85. no_signal, /* signal */
  86. serial_get_fd, /* get_fd */
  87. default_map_access, /* map_access */
  88. default_get_sd, /* get_sd */
  89. default_set_sd, /* set_sd */
  90. no_get_full_name, /* get_full_name */
  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_close_handle, /* close_handle */
  97. serial_destroy /* destroy */
  98. };
  99. static const struct fd_ops serial_fd_ops =
  100. {
  101. default_fd_get_poll_events, /* get_poll_events */
  102. default_poll_event, /* poll_event */
  103. serial_get_fd_type, /* get_fd_type */
  104. no_fd_read, /* read */
  105. no_fd_write, /* write */
  106. no_fd_flush, /* flush */
  107. default_fd_get_file_info, /* get_file_info */
  108. no_fd_get_volume_info, /* get_volume_info */
  109. serial_ioctl, /* ioctl */
  110. serial_queue_async, /* queue_async */
  111. serial_reselect_async /* reselect_async */
  112. };
  113. /* check if the given fd is a serial port */
  114. int is_serial_fd( struct fd *fd )
  115. {
  116. struct termios tios;
  117. return !tcgetattr( get_unix_fd(fd), &tios );
  118. }
  119. /* create a serial object for a given fd */
  120. struct object *create_serial( struct fd *fd )
  121. {
  122. struct serial *serial;
  123. if (!(serial = alloc_object( &serial_ops ))) return NULL;
  124. serial->read_timer = NULL;
  125. serial->eventmask = 0;
  126. serial->generation = 0;
  127. serial->pending_write = 0;
  128. serial->pending_wait = 0;
  129. memset( &serial->timeouts, 0, sizeof(serial->timeouts) );
  130. serial->fd = (struct fd *)grab_object( fd );
  131. set_fd_user( fd, &serial_fd_ops, &serial->obj );
  132. return &serial->obj;
  133. }
  134. static struct fd *serial_get_fd( struct object *obj )
  135. {
  136. struct serial *serial = (struct serial *)obj;
  137. return (struct fd *)grab_object( serial->fd );
  138. }
  139. static void serial_destroy( struct object *obj)
  140. {
  141. struct serial *serial = (struct serial *)obj;
  142. if (serial->read_timer) remove_timeout_user( serial->read_timer );
  143. release_object( serial->fd );
  144. }
  145. static void serial_dump( struct object *obj, int verbose )
  146. {
  147. struct serial *serial = (struct serial *)obj;
  148. assert( obj->ops == &serial_ops );
  149. fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
  150. }
  151. static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
  152. {
  153. return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
  154. }
  155. static enum server_fd_type serial_get_fd_type( struct fd *fd )
  156. {
  157. return FD_TYPE_SERIAL;
  158. }
  159. static int serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  160. {
  161. struct serial *serial = get_fd_user( fd );
  162. switch (code)
  163. {
  164. case IOCTL_SERIAL_GET_TIMEOUTS:
  165. if (get_reply_max_size() < sizeof(serial->timeouts))
  166. {
  167. set_error( STATUS_BUFFER_TOO_SMALL );
  168. return 0;
  169. }
  170. set_reply_data( &serial->timeouts, sizeof(serial->timeouts ));
  171. return 1;
  172. case IOCTL_SERIAL_SET_TIMEOUTS:
  173. if (get_req_data_size() < sizeof(serial->timeouts))
  174. {
  175. set_error( STATUS_BUFFER_TOO_SMALL );
  176. return 0;
  177. }
  178. memcpy( &serial->timeouts, get_req_data(), sizeof(serial->timeouts) );
  179. return 1;
  180. case IOCTL_SERIAL_GET_WAIT_MASK:
  181. if (get_reply_max_size() < sizeof(serial->eventmask))
  182. {
  183. set_error( STATUS_BUFFER_TOO_SMALL );
  184. return 0;
  185. }
  186. set_reply_data( &serial->eventmask, sizeof(serial->eventmask) );
  187. return 1;
  188. case IOCTL_SERIAL_SET_WAIT_MASK:
  189. if (get_req_data_size() < sizeof(serial->eventmask))
  190. {
  191. set_error( STATUS_BUFFER_TOO_SMALL );
  192. return 0;
  193. }
  194. serial->eventmask = *(unsigned int *)get_req_data();
  195. serial->generation++;
  196. fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
  197. return 1;
  198. default:
  199. set_error( STATUS_NOT_SUPPORTED );
  200. return 0;
  201. }
  202. }
  203. static void serial_queue_async( struct fd *fd, struct async *async, int type, int count )
  204. {
  205. struct serial *serial = get_fd_user( fd );
  206. timeout_t timeout = 0;
  207. assert(serial->obj.ops == &serial_ops);
  208. switch (type)
  209. {
  210. case ASYNC_TYPE_READ:
  211. timeout = serial->timeouts.ReadTotalTimeoutConstant +
  212. (timeout_t)serial->timeouts.ReadTotalTimeoutMultiplier * count;
  213. break;
  214. case ASYNC_TYPE_WRITE:
  215. timeout = serial->timeouts.WriteTotalTimeoutConstant +
  216. (timeout_t)serial->timeouts.WriteTotalTimeoutMultiplier * count;
  217. break;
  218. }
  219. fd_queue_async( fd, async, type );
  220. if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
  221. set_error( STATUS_PENDING );
  222. }
  223. static void serial_read_timeout( void *arg )
  224. {
  225. struct serial *serial = arg;
  226. serial->read_timer = NULL;
  227. fd_async_wake_up( serial->fd, ASYNC_TYPE_READ, STATUS_TIMEOUT );
  228. }
  229. static void serial_reselect_async( struct fd *fd, struct async_queue *queue )
  230. {
  231. struct serial *serial = get_fd_user( fd );
  232. if (serial->read_timer)
  233. {
  234. if (!(default_fd_get_poll_events( fd ) & POLLIN))
  235. {
  236. remove_timeout_user( serial->read_timer );
  237. serial->read_timer = NULL;
  238. }
  239. }
  240. else if (serial->timeouts.ReadIntervalTimeout && (default_fd_get_poll_events( fd ) & POLLIN))
  241. {
  242. serial->read_timer = add_timeout_user( (timeout_t)serial->timeouts.ReadIntervalTimeout * -10000,
  243. serial_read_timeout, serial );
  244. }
  245. default_fd_reselect_async( fd, queue );
  246. }
  247. DECL_HANDLER(get_serial_info)
  248. {
  249. struct serial *serial;
  250. if ((serial = get_serial_obj( current->process, req->handle, 0 )))
  251. {
  252. if (req->flags & SERIALINFO_PENDING_WAIT)
  253. {
  254. if (serial->pending_wait)
  255. {
  256. release_object( serial );
  257. set_error( STATUS_INVALID_PARAMETER );
  258. return;
  259. }
  260. serial->pending_wait = 1;
  261. }
  262. /* event mask */
  263. reply->eventmask = serial->eventmask;
  264. reply->cookie = serial->generation;
  265. /* pending write */
  266. reply->pending_write = serial->pending_write;
  267. if (req->flags & SERIALINFO_PENDING_WRITE)
  268. serial->pending_write = 0;
  269. release_object( serial );
  270. }
  271. }
  272. DECL_HANDLER(set_serial_info)
  273. {
  274. struct serial *serial;
  275. if ((serial = get_serial_obj( current->process, req->handle, 0 )))
  276. {
  277. if (req->flags & SERIALINFO_PENDING_WAIT)
  278. {
  279. if (!serial->pending_wait)
  280. {
  281. release_object( serial );
  282. set_error( STATUS_INVALID_PARAMETER );
  283. return;
  284. }
  285. serial->pending_wait = 0;
  286. }
  287. /* pending write */
  288. if (req->flags & SERIALINFO_PENDING_WRITE)
  289. serial->pending_write = 1;
  290. release_object( serial );
  291. }
  292. }