serial.c 9.8 KB

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