serial_tty.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #ifdef LWP_DEBUG_INIT
  57. static volatile int _early_input = 0;
  58. static void _set_debug(rt_device_t dev, rt_size_t size);
  59. RT_OBJECT_HOOKLIST_DEFINE_NODE(rt_hw_serial_rxind, _set_debug_node, _set_debug);
  60. static void _set_debug(rt_device_t dev, rt_size_t size)
  61. {
  62. rt_list_remove(&_set_debug_node.list_node);
  63. _early_input = 1;
  64. }
  65. static void _setup_debug_rxind_hook(void)
  66. {
  67. rt_hw_serial_rxind_sethook(&_set_debug_node);
  68. }
  69. int lwp_startup_debug_request(void)
  70. {
  71. return _early_input;
  72. }
  73. #else /* !LWP_DEBUG_INIT */
  74. static void _setup_debug_rxind_hook(void)
  75. {
  76. return ;
  77. }
  78. #endif /* LWP_DEBUG_INIT */
  79. static void _tty_rx_notify(struct rt_device *device)
  80. {
  81. lwp_tty_t tp;
  82. struct serial_tty_context *softc;
  83. tp = rt_container_of(device, struct lwp_tty, parent);
  84. RT_ASSERT(tp);
  85. softc = tty_softc(tp);
  86. if (_ttyworkq)
  87. rt_workqueue_submit_work(_ttyworkq, &softc->work, 0);
  88. }
  89. static void _tty_rx_worker(struct rt_work *work, void *data)
  90. {
  91. char input;
  92. rt_ssize_t readbytes;
  93. lwp_tty_t tp = data;
  94. struct serial_tty_context *softc;
  95. struct rt_serial_device *serial;
  96. tty_lock(tp);
  97. while (1)
  98. {
  99. softc = tty_softc(tp);
  100. serial = softc->parent;
  101. readbytes = rt_device_read(&serial->parent, -1, &input, 1);
  102. if (readbytes != 1)
  103. {
  104. break;
  105. }
  106. ttydisc_rint(tp, input, 0);
  107. }
  108. ttydisc_rint_done(tp);
  109. tty_unlock(tp);
  110. }
  111. rt_inline void _setup_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  112. struct serial_tty_context *softc)
  113. {
  114. struct rt_device_notify notify;
  115. softc->backup_notify = serial->rx_notify;
  116. notify.dev = &tp->parent;
  117. notify.notify = _tty_rx_notify;
  118. rt_device_init(&serial->parent);
  119. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &notify);
  120. }
  121. rt_inline void _restore_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  122. struct serial_tty_context *softc)
  123. {
  124. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &softc->backup_notify);
  125. }
  126. static int _serial_isbusy(struct rt_serial_device *serial)
  127. {
  128. rt_thread_t user_thread = rt_console_current_user();
  129. rt_thread_t self_thread = rt_thread_self();
  130. return rt_console_get_device() == &serial->parent &&
  131. (user_thread != RT_NULL && user_thread != self_thread);
  132. }
  133. static void serial_tty_outwakeup(struct lwp_tty *tp)
  134. {
  135. char out_char;
  136. int len;
  137. struct serial_tty_context *context = tty_softc(tp);
  138. struct rt_serial_device *device;
  139. if (!context || !context->parent)
  140. {
  141. LOG_E("%s: Data corruption", __func__);
  142. return;
  143. }
  144. device = context->parent;
  145. if (_serial_isbusy(device))
  146. {
  147. return ;
  148. }
  149. while ((len = ttydisc_getc(tp, &out_char, sizeof(out_char))) != 0)
  150. {
  151. device->ops->putc(device, out_char);
  152. /* discard remaining if emergency output is happened */
  153. if (_serial_isbusy(device))
  154. {
  155. break;
  156. }
  157. }
  158. }
  159. static int serial_tty_open(struct lwp_tty *tp)
  160. {
  161. struct serial_tty_context *softc;
  162. struct rt_serial_device *serial;
  163. rt_err_t error;
  164. int oflags;
  165. softc = tty_softc(tp);
  166. serial = softc->parent;
  167. LOG_D("%s", __func__);
  168. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  169. error = rt_device_open(&serial->parent, oflags);
  170. if (!error)
  171. {
  172. /**
  173. * to avoid driver accesssing null data,
  174. * these are setup only after tty is registered
  175. */
  176. _setup_serial(serial, tp, softc);
  177. }
  178. return error;
  179. }
  180. static void serial_tty_close(struct lwp_tty *tp)
  181. {
  182. struct serial_tty_context *softc;
  183. struct rt_serial_device *serial;
  184. softc = tty_softc(tp);
  185. serial = softc->parent;
  186. LOG_D("%s", __func__);
  187. _restore_serial(serial, tp, softc);
  188. rt_device_close(&serial->parent);
  189. }
  190. static int serial_tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
  191. struct rt_thread *td)
  192. {
  193. int error;
  194. switch (cmd)
  195. {
  196. case TCSETS:
  197. case TCSETSW:
  198. case TCSETSF:
  199. RT_ASSERT(tp->t_devswsoftc);
  200. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  201. struct rt_serial_device *serial = softc->parent;
  202. struct termios *termios = (struct termios *)data;
  203. rt_device_control(&(serial->parent), cmd, termios);
  204. error = -ENOIOCTL;
  205. default:
  206. /**
  207. * Note: for the most case, we don't let serial layer handle ioctl,
  208. * for that they can't act properly regarding to the process
  209. * management system, since it is unawared of that. So a ENOSYS is
  210. * returned and caused the TTY layer to handle ioctl itself.
  211. */
  212. error = -ENOSYS;
  213. break;
  214. }
  215. return error;
  216. }
  217. static struct lwp_ttydevsw serial_ttydevsw = {
  218. .tsw_open = serial_tty_open,
  219. .tsw_close = serial_tty_close,
  220. .tsw_ioctl = serial_tty_ioctl,
  221. .tsw_outwakeup = serial_tty_outwakeup,
  222. };
  223. rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
  224. {
  225. rt_err_t rc;
  226. lwp_tty_t tty;
  227. char *dev_name;
  228. struct serial_tty_context *softc;
  229. if (serial->rx_notify.dev)
  230. {
  231. return -RT_EBUSY;
  232. }
  233. softc = rt_malloc(sizeof(struct serial_tty_context));
  234. if (softc)
  235. {
  236. dev_name = alloc_device_name();
  237. if (dev_name)
  238. {
  239. softc->parent = serial;
  240. tty = lwp_tty_create(&serial_ttydevsw, softc);
  241. if (tty)
  242. {
  243. rc = lwp_tty_register(tty, dev_name);
  244. rt_work_init(&softc->work, _tty_rx_worker, tty);
  245. if (rc != RT_EOK)
  246. {
  247. rt_free(tty);
  248. rt_free(softc);
  249. }
  250. }
  251. else
  252. {
  253. rt_free(softc);
  254. rc = -RT_ENOMEM;
  255. }
  256. rt_free(dev_name);
  257. }
  258. else
  259. {
  260. rt_free(softc);
  261. rc = -RT_ENOMEM;
  262. }
  263. }
  264. else
  265. {
  266. rc = -RT_ENOMEM;
  267. }
  268. return rc;
  269. }
  270. rt_err_t rt_hw_serial_unregister_tty(struct rt_serial_device *serial)
  271. {
  272. rt_device_t tty_dev;
  273. lwp_tty_t tp;
  274. struct serial_tty_context *softc;
  275. tty_dev = serial->rx_notify.dev;
  276. tp = rt_container_of(tty_dev, struct lwp_tty, parent);
  277. /* restore serial setting */
  278. softc = tty_softc(tp);
  279. serial->rx_notify = softc->backup_notify;
  280. tty_rel_gone(tp);
  281. /* device unregister? */
  282. rt_device_destroy(&tp->parent);
  283. /* resource free? */
  284. lwp_tty_delete(tp);
  285. return RT_EOK;
  286. }
  287. static int _tty_workqueue_init(void)
  288. {
  289. if (_ttyworkq != RT_NULL)
  290. return RT_EOK;
  291. _ttyworkq = rt_workqueue_create("ttyworkq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
  292. LWP_TTY_WORKQUEUE_PRIORITY);
  293. RT_ASSERT(_ttyworkq != RT_NULL);
  294. _setup_debug_rxind_hook();
  295. return RT_EOK;
  296. }
  297. INIT_PREV_EXPORT(_tty_workqueue_init);