serial_tty.c 9.2 KB

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