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