serial.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <assert.h>
  24. #include <fcntl.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <sys/time.h>
  30. #include <sys/types.h>
  31. #include <sys/ioctl.h>
  32. #include <time.h>
  33. #include <termios.h>
  34. #include <unistd.h>
  35. #include <poll.h>
  36. #ifdef HAVE_UTIME_H
  37. #include <utime.h>
  38. #endif
  39. #include "ntstatus.h"
  40. #define WIN32_NO_STATUS
  41. #include "windef.h"
  42. #include "winternl.h"
  43. #include "winioctl.h"
  44. #include "ddk/ntddser.h"
  45. #include "file.h"
  46. #include "handle.h"
  47. #include "thread.h"
  48. #include "request.h"
  49. static void serial_dump( struct object *obj, int verbose );
  50. static struct fd *serial_get_fd( struct object *obj );
  51. static void serial_destroy(struct object *obj);
  52. static enum server_fd_type serial_get_fd_type( struct fd *fd );
  53. static void serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  54. static void serial_queue_async( struct fd *fd, struct async *async, int type, int count );
  55. static void serial_reselect_async( struct fd *fd, struct async_queue *queue );
  56. struct serial
  57. {
  58. struct object obj;
  59. struct fd *fd;
  60. struct timeout_user *read_timer;
  61. SERIAL_TIMEOUTS timeouts;
  62. unsigned int eventmask;
  63. unsigned int generation; /* event mask change counter */
  64. unsigned int pending_write : 1;
  65. unsigned int pending_wait : 1;
  66. struct termios original;
  67. /* FIXME: add dcb, comm status, handler module, sharing */
  68. };
  69. static const struct object_ops serial_ops =
  70. {
  71. sizeof(struct serial), /* size */
  72. &file_type, /* type */
  73. serial_dump, /* dump */
  74. add_queue, /* add_queue */
  75. remove_queue, /* remove_queue */
  76. default_fd_signaled, /* signaled */
  77. no_satisfied, /* satisfied */
  78. no_signal, /* signal */
  79. serial_get_fd, /* get_fd */
  80. default_map_access, /* map_access */
  81. default_get_sd, /* get_sd */
  82. default_set_sd, /* set_sd */
  83. no_get_full_name, /* get_full_name */
  84. no_lookup_name, /* lookup_name */
  85. no_link_name, /* link_name */
  86. NULL, /* unlink_name */
  87. no_open_file, /* open_file */
  88. no_kernel_obj_list, /* get_kernel_obj_list */
  89. default_fd_get_fast_sync, /* get_fast_sync */
  90. no_close_handle, /* close_handle */
  91. serial_destroy /* destroy */
  92. };
  93. static const struct fd_ops serial_fd_ops =
  94. {
  95. default_fd_get_poll_events, /* get_poll_events */
  96. default_poll_event, /* poll_event */
  97. serial_get_fd_type, /* get_fd_type */
  98. no_fd_read, /* read */
  99. no_fd_write, /* write */
  100. no_fd_flush, /* flush */
  101. default_fd_get_file_info, /* get_file_info */
  102. no_fd_get_volume_info, /* get_volume_info */
  103. serial_ioctl, /* ioctl */
  104. default_fd_cancel_async, /* cancel_async */
  105. serial_queue_async, /* queue_async */
  106. serial_reselect_async /* reselect_async */
  107. };
  108. /* check if the given fd is a serial port */
  109. int is_serial_fd( struct fd *fd )
  110. {
  111. struct termios tios;
  112. return !tcgetattr( get_unix_fd(fd), &tios );
  113. }
  114. /* create a serial object for a given fd */
  115. struct object *create_serial( struct fd *fd )
  116. {
  117. struct serial *serial;
  118. if (!(serial = alloc_object( &serial_ops ))) return NULL;
  119. serial->read_timer = NULL;
  120. serial->eventmask = 0;
  121. serial->generation = 0;
  122. serial->pending_write = 0;
  123. serial->pending_wait = 0;
  124. memset( &serial->timeouts, 0, sizeof(serial->timeouts) );
  125. serial->fd = (struct fd *)grab_object( fd );
  126. set_fd_user( fd, &serial_fd_ops, &serial->obj );
  127. return &serial->obj;
  128. }
  129. static struct fd *serial_get_fd( struct object *obj )
  130. {
  131. struct serial *serial = (struct serial *)obj;
  132. return (struct fd *)grab_object( serial->fd );
  133. }
  134. static void serial_destroy( struct object *obj)
  135. {
  136. struct serial *serial = (struct serial *)obj;
  137. if (serial->read_timer) remove_timeout_user( serial->read_timer );
  138. release_object( serial->fd );
  139. }
  140. static void serial_dump( struct object *obj, int verbose )
  141. {
  142. struct serial *serial = (struct serial *)obj;
  143. assert( obj->ops == &serial_ops );
  144. fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
  145. }
  146. static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
  147. {
  148. return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
  149. }
  150. static enum server_fd_type serial_get_fd_type( struct fd *fd )
  151. {
  152. return FD_TYPE_SERIAL;
  153. }
  154. static void serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  155. {
  156. struct serial *serial = get_fd_user( fd );
  157. switch (code)
  158. {
  159. case IOCTL_SERIAL_GET_TIMEOUTS:
  160. if (get_reply_max_size() < sizeof(serial->timeouts))
  161. {
  162. set_error( STATUS_BUFFER_TOO_SMALL );
  163. return;
  164. }
  165. set_reply_data( &serial->timeouts, sizeof(serial->timeouts ));
  166. return;
  167. case IOCTL_SERIAL_SET_TIMEOUTS:
  168. if (get_req_data_size() < sizeof(serial->timeouts))
  169. {
  170. set_error( STATUS_BUFFER_TOO_SMALL );
  171. return;
  172. }
  173. memcpy( &serial->timeouts, get_req_data(), sizeof(serial->timeouts) );
  174. return;
  175. case IOCTL_SERIAL_GET_WAIT_MASK:
  176. if (get_reply_max_size() < sizeof(serial->eventmask))
  177. {
  178. set_error( STATUS_BUFFER_TOO_SMALL );
  179. return;
  180. }
  181. set_reply_data( &serial->eventmask, sizeof(serial->eventmask) );
  182. return;
  183. case IOCTL_SERIAL_SET_WAIT_MASK:
  184. if (get_req_data_size() < sizeof(serial->eventmask))
  185. {
  186. set_error( STATUS_BUFFER_TOO_SMALL );
  187. return;
  188. }
  189. serial->eventmask = *(unsigned int *)get_req_data();
  190. serial->generation++;
  191. fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
  192. return;
  193. default:
  194. set_error( STATUS_NOT_SUPPORTED );
  195. }
  196. }
  197. static void serial_queue_async( struct fd *fd, struct async *async, int type, int count )
  198. {
  199. struct serial *serial = get_fd_user( fd );
  200. timeout_t timeout = 0;
  201. assert(serial->obj.ops == &serial_ops);
  202. switch (type)
  203. {
  204. case ASYNC_TYPE_READ:
  205. timeout = serial->timeouts.ReadTotalTimeoutConstant +
  206. (timeout_t)serial->timeouts.ReadTotalTimeoutMultiplier * count;
  207. break;
  208. case ASYNC_TYPE_WRITE:
  209. timeout = serial->timeouts.WriteTotalTimeoutConstant +
  210. (timeout_t)serial->timeouts.WriteTotalTimeoutMultiplier * count;
  211. break;
  212. }
  213. fd_queue_async( fd, async, type );
  214. if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
  215. set_error( STATUS_PENDING );
  216. }
  217. static void serial_read_timeout( void *arg )
  218. {
  219. struct serial *serial = arg;
  220. serial->read_timer = NULL;
  221. fd_async_wake_up( serial->fd, ASYNC_TYPE_READ, STATUS_TIMEOUT );
  222. }
  223. static void serial_reselect_async( struct fd *fd, struct async_queue *queue )
  224. {
  225. struct serial *serial = get_fd_user( fd );
  226. if (serial->read_timer)
  227. {
  228. if (!(default_fd_get_poll_events( fd ) & POLLIN))
  229. {
  230. remove_timeout_user( serial->read_timer );
  231. serial->read_timer = NULL;
  232. }
  233. }
  234. else if (serial->timeouts.ReadIntervalTimeout && (default_fd_get_poll_events( fd ) & POLLIN))
  235. {
  236. serial->read_timer = add_timeout_user( (timeout_t)serial->timeouts.ReadIntervalTimeout * -10000,
  237. serial_read_timeout, serial );
  238. }
  239. default_fd_reselect_async( fd, queue );
  240. }
  241. DECL_HANDLER(get_serial_info)
  242. {
  243. struct serial *serial;
  244. if ((serial = get_serial_obj( current->process, req->handle, 0 )))
  245. {
  246. if (req->flags & SERIALINFO_PENDING_WAIT)
  247. {
  248. if (serial->pending_wait)
  249. {
  250. release_object( serial );
  251. set_error( STATUS_INVALID_PARAMETER );
  252. return;
  253. }
  254. serial->pending_wait = 1;
  255. }
  256. /* event mask */
  257. reply->eventmask = serial->eventmask;
  258. reply->cookie = serial->generation;
  259. /* pending write */
  260. reply->pending_write = serial->pending_write;
  261. if (req->flags & SERIALINFO_PENDING_WRITE)
  262. serial->pending_write = 0;
  263. release_object( serial );
  264. }
  265. }
  266. DECL_HANDLER(set_serial_info)
  267. {
  268. struct serial *serial;
  269. if ((serial = get_serial_obj( current->process, req->handle, 0 )))
  270. {
  271. if (req->flags & SERIALINFO_PENDING_WAIT)
  272. {
  273. if (!serial->pending_wait)
  274. {
  275. release_object( serial );
  276. set_error( STATUS_INVALID_PARAMETER );
  277. return;
  278. }
  279. serial->pending_wait = 0;
  280. }
  281. /* pending write */
  282. if (req->flags & SERIALINFO_PENDING_WRITE)
  283. serial->pending_write = 1;
  284. release_object( serial );
  285. }
  286. }