serial_tty.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-21 Shell init ver.
  9. */
  10. #define DBG_TAG "drivers.serial"
  11. #define DBG_LVL DBG_INFO
  12. #include <rtdbg.h>
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include <terminal/terminal.h>
  17. #define TTY_NAME_PREFIX "S" /* (S)erial */
  18. #define LWP_TTY_WORKQUEUE_PRIORITY 3
  19. struct serial_tty_context
  20. {
  21. struct rt_serial_device *parent;
  22. struct rt_device_notify backup_notify;
  23. struct rt_work work;
  24. };
  25. static struct rt_workqueue *_ttyworkq; /* system work queue */
  26. static rt_atomic_t _device_id_counter = 0;
  27. static long get_dec_digits(rt_ubase_t val)
  28. {
  29. long result = 1;
  30. while (1)
  31. {
  32. if (val < 10)
  33. return result;
  34. if (val < 100)
  35. return result + 1;
  36. if (val < 1000)
  37. return result + 2;
  38. if (val < 10000)
  39. return result + 3;
  40. val /= 10000U;
  41. result += 4;
  42. }
  43. return result;
  44. }
  45. static char *alloc_device_name(void)
  46. {
  47. char *tty_dev_name;
  48. unsigned int devid = rt_atomic_add(&_device_id_counter, 1);
  49. long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
  50. + get_dec_digits(devid) + 1; /* tailing \0 */
  51. tty_dev_name = rt_malloc(digits_len);
  52. if (tty_dev_name)
  53. rt_sprintf(tty_dev_name, "%s%u", TTY_NAME_PREFIX, devid);
  54. return tty_dev_name;
  55. }
  56. static void _tty_rx_notify(struct rt_device *device)
  57. {
  58. lwp_tty_t tp;
  59. struct serial_tty_context *softc;
  60. tp = rt_container_of(device, struct lwp_tty, parent);
  61. RT_ASSERT(tp);
  62. softc = tty_softc(tp);
  63. if (_ttyworkq)
  64. rt_workqueue_submit_work(_ttyworkq, &softc->work, 0);
  65. }
  66. static void _tty_rx_worker(struct rt_work *work, void *data)
  67. {
  68. char input;
  69. rt_ssize_t readbytes;
  70. lwp_tty_t tp = data;
  71. struct serial_tty_context *softc;
  72. struct rt_serial_device *serial;
  73. tty_lock(tp);
  74. while (1)
  75. {
  76. softc = tty_softc(tp);
  77. serial = softc->parent;
  78. readbytes = rt_device_read(&serial->parent, -1, &input, 1);
  79. if (readbytes != 1)
  80. {
  81. break;
  82. }
  83. ttydisc_rint(tp, input, 0);
  84. }
  85. ttydisc_rint_done(tp);
  86. tty_unlock(tp);
  87. }
  88. rt_inline void _setup_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  89. struct serial_tty_context *softc)
  90. {
  91. struct rt_device_notify notify;
  92. softc->backup_notify = serial->rx_notify;
  93. notify.dev = &tp->parent;
  94. notify.notify = _tty_rx_notify;
  95. rt_device_init(&serial->parent);
  96. rt_work_init(&softc->work, _tty_rx_worker, tp);
  97. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &notify);
  98. }
  99. rt_inline void _restore_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  100. struct serial_tty_context *softc)
  101. {
  102. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &softc->backup_notify);
  103. }
  104. static int _serial_isbusy(struct rt_serial_device *serial)
  105. {
  106. rt_thread_t user_thread = rt_console_current_user();
  107. rt_thread_t self_thread = rt_thread_self();
  108. return rt_console_get_device() == &serial->parent &&
  109. (user_thread != RT_NULL && user_thread != self_thread);
  110. }
  111. static void serial_tty_outwakeup(struct lwp_tty *tp)
  112. {
  113. char out_char;
  114. int len;
  115. struct serial_tty_context *context = tty_softc(tp);
  116. struct rt_serial_device *device;
  117. if (!context || !context->parent)
  118. {
  119. LOG_E("%s: Data corruption", __func__);
  120. return;
  121. }
  122. device = context->parent;
  123. if (_serial_isbusy(device))
  124. {
  125. return ;
  126. }
  127. while ((len = ttydisc_getc(tp, &out_char, sizeof(out_char))) != 0)
  128. {
  129. device->ops->putc(device, out_char);
  130. /* discard remaining if emergency output is happened */
  131. if (_serial_isbusy(device))
  132. {
  133. break;
  134. }
  135. }
  136. }
  137. static int serial_tty_open(struct lwp_tty *tp)
  138. {
  139. struct serial_tty_context *softc;
  140. struct rt_serial_device *serial;
  141. rt_err_t error;
  142. int oflags;
  143. softc = tty_softc(tp);
  144. serial = softc->parent;
  145. LOG_D("%s", __func__);
  146. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  147. error = rt_device_open(&serial->parent, oflags);
  148. if (!error)
  149. {
  150. /**
  151. * to avoid driver accesssing null data,
  152. * these are setup only after tty is registered
  153. */
  154. _setup_serial(serial, tp, softc);
  155. }
  156. return error;
  157. }
  158. static void serial_tty_close(struct lwp_tty *tp)
  159. {
  160. struct serial_tty_context *softc;
  161. struct rt_serial_device *serial;
  162. softc = tty_softc(tp);
  163. serial = softc->parent;
  164. LOG_D("%s", __func__);
  165. _restore_serial(serial, tp, softc);
  166. rt_device_close(&serial->parent);
  167. }
  168. static int serial_tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
  169. struct rt_thread *td)
  170. {
  171. int error;
  172. switch (cmd)
  173. {
  174. case TCSETS:
  175. case TCSETSW:
  176. case TCSETSF:
  177. RT_ASSERT(tp->t_devswsoftc);
  178. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  179. struct rt_serial_device *serial = softc->parent;
  180. struct termios *termios = (struct termios *)data;
  181. rt_device_control(&(serial->parent), cmd, termios);
  182. error = -ENOIOCTL;
  183. default:
  184. /**
  185. * Note: for the most case, we don't let serial layer handle ioctl,
  186. * for that they can't act properly regarding to the process
  187. * management system, since it is unawared of that. So a ENOSYS is
  188. * returned and caused the TTY layer to handle ioctl itself.
  189. */
  190. error = -ENOSYS;
  191. break;
  192. }
  193. return error;
  194. }
  195. static struct lwp_ttydevsw serial_ttydevsw = {
  196. .tsw_open = serial_tty_open,
  197. .tsw_close = serial_tty_close,
  198. .tsw_ioctl = serial_tty_ioctl,
  199. .tsw_outwakeup = serial_tty_outwakeup,
  200. };
  201. rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
  202. {
  203. rt_err_t rc;
  204. lwp_tty_t tty;
  205. char *dev_name;
  206. struct serial_tty_context *softc;
  207. if (serial->rx_notify.dev)
  208. {
  209. return -RT_EBUSY;
  210. }
  211. softc = rt_malloc(sizeof(struct serial_tty_context));
  212. if (softc)
  213. {
  214. dev_name = alloc_device_name();
  215. if (dev_name)
  216. {
  217. softc->parent = serial;
  218. tty = lwp_tty_create(&serial_ttydevsw, softc);
  219. if (tty)
  220. {
  221. rc = lwp_tty_register(tty, dev_name);
  222. if (rc != RT_EOK)
  223. {
  224. rt_free(tty);
  225. rt_free(softc);
  226. }
  227. }
  228. else
  229. {
  230. rt_free(softc);
  231. rc = -RT_ENOMEM;
  232. }
  233. rt_free(dev_name);
  234. }
  235. else
  236. {
  237. rt_free(softc);
  238. rc = -RT_ENOMEM;
  239. }
  240. }
  241. else
  242. {
  243. rc = -RT_ENOMEM;
  244. }
  245. return rc;
  246. }
  247. rt_err_t rt_hw_serial_unregister_tty(struct rt_serial_device *serial)
  248. {
  249. rt_device_t tty_dev;
  250. lwp_tty_t tp;
  251. struct serial_tty_context *softc;
  252. tty_dev = serial->rx_notify.dev;
  253. tp = rt_container_of(tty_dev, struct lwp_tty, parent);
  254. /* restore serial setting */
  255. softc = tty_softc(tp);
  256. serial->rx_notify = softc->backup_notify;
  257. tty_rel_gone(tp);
  258. /* device unregister? */
  259. rt_device_destroy(&tp->parent);
  260. /* resource free? */
  261. lwp_tty_delete(tp);
  262. return RT_EOK;
  263. }
  264. static int _tty_workqueue_init(void)
  265. {
  266. if (_ttyworkq != RT_NULL)
  267. return RT_EOK;
  268. _ttyworkq = rt_workqueue_create("ttyworkq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
  269. LWP_TTY_WORKQUEUE_PRIORITY);
  270. RT_ASSERT(_ttyworkq != RT_NULL);
  271. return RT_EOK;
  272. }
  273. INIT_PREV_EXPORT(_tty_workqueue_init);