serial_tty.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. if (!serial_name || rt_strlen(serial_name) < 4 || rt_strncmp(serial_name, "uart", 4))
  58. {
  59. return RT_NULL;
  60. }
  61. long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
  62. + strlen(serial_name + sizeof("uart") - 1) /* suffix of serial device name*/
  63. + 1; /* tailing \0 */
  64. tty_dev_name = rt_malloc(digits_len);
  65. if (tty_dev_name)
  66. rt_sprintf(tty_dev_name, "%s%s", TTY_NAME_PREFIX, serial_name + sizeof("uart") - 1);
  67. #else
  68. RT_UNUSED(serial);
  69. unsigned int devid = rt_atomic_add(&_device_id_counter, 1);
  70. long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
  71. + get_dec_digits(devid) + 1; /* tailing \0 */
  72. tty_dev_name = rt_malloc(digits_len);
  73. if (tty_dev_name)
  74. rt_sprintf(tty_dev_name, "%s%u", TTY_NAME_PREFIX, devid);
  75. #endif
  76. return tty_dev_name;
  77. }
  78. #ifdef LWP_DEBUG_INIT
  79. static volatile int _early_input = 0;
  80. static void _set_debug(rt_device_t dev, rt_size_t size);
  81. RT_OBJECT_HOOKLIST_DEFINE_NODE(rt_hw_serial_rxind, _set_debug_node, _set_debug);
  82. static void _set_debug(rt_device_t dev, rt_size_t size)
  83. {
  84. rt_list_remove(&_set_debug_node.list_node);
  85. _early_input = 1;
  86. }
  87. static void _setup_debug_rxind_hook(void)
  88. {
  89. rt_hw_serial_rxind_sethook(&_set_debug_node);
  90. }
  91. int lwp_startup_debug_request(void)
  92. {
  93. return _early_input;
  94. }
  95. #else /* !LWP_DEBUG_INIT */
  96. static void _setup_debug_rxind_hook(void)
  97. {
  98. return ;
  99. }
  100. #endif /* LWP_DEBUG_INIT */
  101. static rt_err_t _serial_ty_bypass(struct rt_serial_device* serial, char ch,void *data)
  102. {
  103. lwp_tty_t tp;
  104. tp = (lwp_tty_t)data;
  105. tty_lock(tp);
  106. ttydisc_rint(tp, ch, 0);
  107. ttydisc_rint_done(tp);
  108. tty_unlock(tp);
  109. return RT_EOK;
  110. }
  111. rt_inline void _setup_serial(struct rt_serial_device* serial, lwp_tty_t tp,
  112. struct serial_tty_context *softc)
  113. {
  114. rt_bypass_lower_register(serial, "tty", RT_BYPASS_PROTECT_LEVEL_1, _serial_ty_bypass, (void *)tp);
  115. }
  116. rt_inline void _restore_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  117. struct serial_tty_context *softc)
  118. {
  119. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &softc->backup_notify);
  120. }
  121. static void _serial_tty_set_speed(struct lwp_tty *tp)
  122. {
  123. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  124. struct rt_serial_device *serial;
  125. struct termios serial_hw_config;
  126. RT_ASSERT(softc);
  127. serial = softc->parent;
  128. rt_device_control(&(serial->parent), TCGETS, &serial_hw_config);
  129. tp->t_termios_init_in.c_cflag |= serial_hw_config.c_cflag;
  130. tp->t_termios_init_in.__c_ispeed = tp->t_termios_init_in.__c_ospeed = cfgetospeed(&tp->t_termios_init_in);
  131. }
  132. static int _serial_isbusy(struct rt_serial_device *serial)
  133. {
  134. rt_thread_t user_thread = rt_console_current_user();
  135. rt_thread_t self_thread = rt_thread_self();
  136. return rt_console_get_device() == &serial->parent &&
  137. (user_thread != RT_NULL && user_thread != self_thread);
  138. }
  139. static void serial_tty_outwakeup(struct lwp_tty *tp)
  140. {
  141. char out_char;
  142. int len;
  143. struct serial_tty_context *context = tty_softc(tp);
  144. struct rt_serial_device *device;
  145. if (!context || !context->parent)
  146. {
  147. LOG_E("%s: Data corruption", __func__);
  148. return;
  149. }
  150. device = context->parent;
  151. if (_serial_isbusy(device))
  152. {
  153. return ;
  154. }
  155. while ((len = ttydisc_getc(tp, &out_char, sizeof(out_char))) != 0)
  156. {
  157. device->ops->putc(device, out_char);
  158. /* discard remaining if emergency output is happened */
  159. if (_serial_isbusy(device))
  160. {
  161. break;
  162. }
  163. }
  164. }
  165. static int serial_tty_open(struct lwp_tty *tp)
  166. {
  167. struct serial_tty_context *softc;
  168. struct rt_serial_device *serial;
  169. rt_err_t error;
  170. int oflags;
  171. softc = tty_softc(tp);
  172. serial = softc->parent;
  173. LOG_D("%s", __func__);
  174. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  175. error = rt_device_open(&serial->parent, oflags);
  176. if (!error)
  177. {
  178. /**
  179. * to avoid driver accesssing null data,
  180. * these are setup only after tty is registered
  181. */
  182. _setup_serial(serial, tp, softc);
  183. }
  184. return error;
  185. }
  186. static void serial_tty_close(struct lwp_tty *tp)
  187. {
  188. struct serial_tty_context *softc;
  189. struct rt_serial_device *serial;
  190. softc = tty_softc(tp);
  191. serial = softc->parent;
  192. LOG_D("%s", __func__);
  193. rt_bypass_lower_unregister(serial, RT_BYPASS_PROTECT_LEVEL_1);
  194. rt_device_close(&serial->parent);
  195. }
  196. static int serial_tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
  197. struct rt_thread *td)
  198. {
  199. int error;
  200. switch (cmd)
  201. {
  202. default:
  203. /**
  204. * Note: for the most case, we don't let serial layer handle ioctl,
  205. * for that they can't act properly regarding to the process
  206. * management system, since it is unawared of that. So a ENOSYS is
  207. * returned and caused the TTY layer to handle ioctl itself.
  208. */
  209. error = -ENOSYS;
  210. break;
  211. }
  212. return error;
  213. }
  214. static int serial_tty_param(struct lwp_tty *tp, struct termios *t)
  215. {
  216. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  217. struct rt_serial_device *serial;
  218. RT_ASSERT(softc);
  219. serial = softc->parent;
  220. if (!tty_opened(tp))
  221. {
  222. /**
  223. * skip configure on open since all configs are copied from the current
  224. * configuration on device. So we don't bother to set it back to device
  225. * again.
  226. */
  227. return RT_EOK;
  228. }
  229. cfsetispeed(t, t->__c_ispeed);
  230. return rt_device_control(&(serial->parent), TCSETS, t);
  231. }
  232. static struct lwp_ttydevsw serial_ttydevsw = {
  233. .tsw_open = serial_tty_open,
  234. .tsw_close = serial_tty_close,
  235. .tsw_ioctl = serial_tty_ioctl,
  236. .tsw_param = serial_tty_param,
  237. .tsw_outwakeup = serial_tty_outwakeup,
  238. };
  239. rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
  240. {
  241. rt_err_t rc;
  242. lwp_tty_t tty;
  243. char *dev_name;
  244. struct serial_tty_context *softc;
  245. if (serial->rx_notify.dev)
  246. {
  247. return -RT_EBUSY;
  248. }
  249. softc = rt_malloc(sizeof(struct serial_tty_context));
  250. if (softc)
  251. {
  252. dev_name = alloc_device_name(serial);
  253. if (dev_name)
  254. {
  255. softc->parent = serial;
  256. tty = lwp_tty_create(&serial_ttydevsw, softc);
  257. if (tty)
  258. {
  259. _serial_tty_set_speed(tty);
  260. rc = lwp_tty_register(tty, dev_name);
  261. if (rc != RT_EOK)
  262. {
  263. rt_free(tty);
  264. rt_free(softc);
  265. }
  266. }
  267. else
  268. {
  269. rt_free(softc);
  270. rc = -RT_ENOMEM;
  271. }
  272. rt_free(dev_name);
  273. }
  274. else
  275. {
  276. rt_free(softc);
  277. rc = -RT_ENOMEM;
  278. }
  279. }
  280. else
  281. {
  282. rc = -RT_ENOMEM;
  283. }
  284. return rc;
  285. }
  286. rt_err_t rt_hw_serial_unregister_tty(struct rt_serial_device *serial)
  287. {
  288. rt_device_t tty_dev;
  289. lwp_tty_t tp;
  290. struct serial_tty_context *softc;
  291. tty_dev = serial->rx_notify.dev;
  292. tp = rt_container_of(tty_dev, struct lwp_tty, parent);
  293. /* restore serial setting */
  294. softc = tty_softc(tp);
  295. serial->rx_notify = softc->backup_notify;
  296. tty_lock(tp);
  297. tty_rel_gone(tp);
  298. /* device unregister? */
  299. rt_device_destroy(&tp->parent);
  300. /* resource free? */
  301. lwp_tty_delete(tp);
  302. return RT_EOK;
  303. }
  304. static int _tty_workqueue_init(void)
  305. {
  306. if (_ttyworkq != RT_NULL)
  307. return RT_EOK;
  308. _ttyworkq = rt_workqueue_create("ttyworkq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
  309. LWP_TTY_WORKQUEUE_PRIORITY);
  310. RT_ASSERT(_ttyworkq != RT_NULL);
  311. _setup_debug_rxind_hook();
  312. return RT_EOK;
  313. }
  314. INIT_PREV_EXPORT(_tty_workqueue_init);
  315. static rt_err_t _match_tty_iter(struct rt_object *obj, void *data)
  316. {
  317. rt_device_t target = *(rt_device_t *)data;
  318. rt_device_t device = rt_container_of(obj, struct rt_device, parent);
  319. if (device->type == RT_Device_Class_Char)
  320. {
  321. lwp_tty_t tp;
  322. if (rt_strncmp(obj->name, "tty"TTY_NAME_PREFIX,
  323. sizeof("tty"TTY_NAME_PREFIX) - 1) == 0)
  324. {
  325. struct serial_tty_context *softc;
  326. tp = rt_container_of(device, struct lwp_tty, parent);
  327. softc = tty_softc(tp);
  328. if (&softc->parent->parent == target)
  329. {
  330. /* matched, early return */
  331. *(rt_device_t *)data = device;
  332. return 1;
  333. }
  334. }
  335. }
  336. return RT_EOK;
  337. }
  338. /**
  339. * @brief The default console is only a backup device with lowest priority.
  340. * It's always recommended to scratch the console from the boot arguments.
  341. * And dont forget to register the device with a higher priority.
  342. */
  343. static int _default_console_setup(void)
  344. {
  345. rt_err_t rc;
  346. rt_device_t bakdev;
  347. rt_device_t ttydev;
  348. bakdev = rt_console_get_device();
  349. if (!bakdev)
  350. {
  351. return -RT_ENOENT;
  352. }
  353. ttydev = bakdev;
  354. rt_object_for_each(RT_Object_Class_Device, _match_tty_iter, &ttydev);
  355. if (ttydev != bakdev)
  356. {
  357. LOG_I("Using /dev/%.*s as default console", RT_NAME_MAX, ttydev->parent.name);
  358. lwp_console_register_backend(ttydev, LWP_CONSOLE_LOWEST_PRIOR);
  359. rc = RT_EOK;
  360. }
  361. else
  362. {
  363. rc = -RT_EINVAL;
  364. }
  365. return rc;
  366. }
  367. INIT_COMPONENT_EXPORT(_default_console_setup);