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_work_init(&softc->work, _tty_rx_worker, tp);
  120. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &notify);
  121. }
  122. rt_inline void _restore_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  123. struct serial_tty_context *softc)
  124. {
  125. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &softc->backup_notify);
  126. }
  127. static int _serial_isbusy(struct rt_serial_device *serial)
  128. {
  129. rt_thread_t user_thread = rt_console_current_user();
  130. rt_thread_t self_thread = rt_thread_self();
  131. return rt_console_get_device() == &serial->parent &&
  132. (user_thread != RT_NULL && user_thread != self_thread);
  133. }
  134. static void serial_tty_outwakeup(struct lwp_tty *tp)
  135. {
  136. char out_char;
  137. int len;
  138. struct serial_tty_context *context = tty_softc(tp);
  139. struct rt_serial_device *device;
  140. if (!context || !context->parent)
  141. {
  142. LOG_E("%s: Data corruption", __func__);
  143. return;
  144. }
  145. device = context->parent;
  146. if (_serial_isbusy(device))
  147. {
  148. return ;
  149. }
  150. while ((len = ttydisc_getc(tp, &out_char, sizeof(out_char))) != 0)
  151. {
  152. device->ops->putc(device, out_char);
  153. /* discard remaining if emergency output is happened */
  154. if (_serial_isbusy(device))
  155. {
  156. break;
  157. }
  158. }
  159. }
  160. static int serial_tty_open(struct lwp_tty *tp)
  161. {
  162. struct serial_tty_context *softc;
  163. struct rt_serial_device *serial;
  164. rt_err_t error;
  165. int oflags;
  166. softc = tty_softc(tp);
  167. serial = softc->parent;
  168. LOG_D("%s", __func__);
  169. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  170. error = rt_device_open(&serial->parent, oflags);
  171. if (!error)
  172. {
  173. /**
  174. * to avoid driver accesssing null data,
  175. * these are setup only after tty is registered
  176. */
  177. _setup_serial(serial, tp, softc);
  178. }
  179. return error;
  180. }
  181. static void serial_tty_close(struct lwp_tty *tp)
  182. {
  183. struct serial_tty_context *softc;
  184. struct rt_serial_device *serial;
  185. softc = tty_softc(tp);
  186. serial = softc->parent;
  187. LOG_D("%s", __func__);
  188. _restore_serial(serial, tp, softc);
  189. rt_device_close(&serial->parent);
  190. }
  191. static int serial_tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
  192. struct rt_thread *td)
  193. {
  194. int error;
  195. switch (cmd)
  196. {
  197. case TCSETS:
  198. case TCSETSW:
  199. case TCSETSF:
  200. RT_ASSERT(tp->t_devswsoftc);
  201. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  202. struct rt_serial_device *serial = softc->parent;
  203. struct termios *termios = (struct termios *)data;
  204. rt_device_control(&(serial->parent), cmd, termios);
  205. error = -ENOIOCTL;
  206. default:
  207. /**
  208. * Note: for the most case, we don't let serial layer handle ioctl,
  209. * for that they can't act properly regarding to the process
  210. * management system, since it is unawared of that. So a ENOSYS is
  211. * returned and caused the TTY layer to handle ioctl itself.
  212. */
  213. error = -ENOSYS;
  214. break;
  215. }
  216. return error;
  217. }
  218. static struct lwp_ttydevsw serial_ttydevsw = {
  219. .tsw_open = serial_tty_open,
  220. .tsw_close = serial_tty_close,
  221. .tsw_ioctl = serial_tty_ioctl,
  222. .tsw_outwakeup = serial_tty_outwakeup,
  223. };
  224. rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
  225. {
  226. rt_err_t rc;
  227. lwp_tty_t tty;
  228. char *dev_name;
  229. struct serial_tty_context *softc;
  230. if (serial->rx_notify.dev)
  231. {
  232. return -RT_EBUSY;
  233. }
  234. softc = rt_malloc(sizeof(struct serial_tty_context));
  235. if (softc)
  236. {
  237. dev_name = alloc_device_name();
  238. if (dev_name)
  239. {
  240. softc->parent = serial;
  241. tty = lwp_tty_create(&serial_ttydevsw, softc);
  242. if (tty)
  243. {
  244. rc = lwp_tty_register(tty, dev_name);
  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);