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
11 #define DBG_TAG "drivers.serial"
12 #define DBG_LVL DBG_INFO
13 #include <rtdbg.h>
14
15 #include <rthw.h>
16 #include <rtthread.h>
17 #include <rtdevice.h>
18 #include <terminal/terminal.h>
19
20 #define TTY_NAME_PREFIX "S" /* (S)erial */
21 #define LWP_TTY_WORKQUEUE_PRIORITY 3
22
23 struct serial_tty_context
24 {
25 struct rt_serial_device *parent;
26 struct rt_device_notify backup_notify;
27 struct rt_work work;
28 };
29
30 static struct rt_workqueue *_ttyworkq; /* system work queue */
31
32 #ifndef RT_USING_DM
33 static rt_atomic_t _device_id_counter = 0;
34
get_dec_digits(rt_ubase_t val)35 static long get_dec_digits(rt_ubase_t val)
36 {
37 long result = 1;
38 while (1)
39 {
40 if (val < 10)
41 return result;
42 if (val < 100)
43 return result + 1;
44 if (val < 1000)
45 return result + 2;
46 if (val < 10000)
47 return result + 3;
48 val /= 10000U;
49 result += 4;
50 }
51 return result;
52 }
53 #endif
54
alloc_device_name(struct rt_serial_device * serial)55 static char *alloc_device_name(struct rt_serial_device *serial)
56 {
57 char *tty_dev_name;
58 #ifdef RT_USING_DM
59 char *serial_name = serial->parent.parent.name;
60 /*
61 * if RT_USING_DM is defined, the name of the serial device
62 * must be obtained using the serial_dev_set_name function,
63 * and it should begin with "uart".
64 */
65 RT_ASSERT((strlen(serial_name) > strlen("uart")) && (strncmp(serial_name, "uart", 4) == 0));
66 long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
67 + strlen(serial_name + sizeof("uart") - 1) /* suffix of serial device name*/
68 + 1; /* tailing \0 */
69
70 tty_dev_name = rt_malloc(digits_len);
71 if (tty_dev_name)
72 rt_sprintf(tty_dev_name, "%s%s", TTY_NAME_PREFIX, serial_name + sizeof("uart") - 1);
73 #else
74 RT_UNUSED(serial);
75 unsigned int devid = rt_atomic_add(&_device_id_counter, 1);
76 long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
77 + get_dec_digits(devid) + 1; /* tailing \0 */
78
79 tty_dev_name = rt_malloc(digits_len);
80 if (tty_dev_name)
81 rt_sprintf(tty_dev_name, "%s%u", TTY_NAME_PREFIX, devid);
82 #endif
83 return tty_dev_name;
84 }
85
86 #ifdef LWP_DEBUG_INIT
87 static volatile int _early_input = 0;
88
89 static void _set_debug(rt_device_t dev, rt_size_t size);
90 RT_OBJECT_HOOKLIST_DEFINE_NODE(rt_hw_serial_rxind, _set_debug_node, _set_debug);
91
_set_debug(rt_device_t dev,rt_size_t size)92 static void _set_debug(rt_device_t dev, rt_size_t size)
93 {
94 rt_list_remove(&_set_debug_node.list_node);
95 _early_input = 1;
96 }
97
_setup_debug_rxind_hook(void)98 static void _setup_debug_rxind_hook(void)
99 {
100 rt_hw_serial_rxind_sethook(&_set_debug_node);
101 }
102
lwp_startup_debug_request(void)103 int lwp_startup_debug_request(void)
104 {
105 return _early_input;
106 }
107
108 #else /* !LWP_DEBUG_INIT */
109
_setup_debug_rxind_hook(void)110 static void _setup_debug_rxind_hook(void)
111 {
112 return ;
113 }
114
115 #endif /* LWP_DEBUG_INIT */
116
_serial_ty_bypass(struct rt_serial_device * serial,char ch,void * data)117 static rt_err_t _serial_ty_bypass(struct rt_serial_device* serial, char ch,void *data)
118 {
119 lwp_tty_t tp;
120 tp = (lwp_tty_t)data;
121
122 tty_lock(tp);
123 ttydisc_rint(tp, ch, 0);
124 ttydisc_rint_done(tp);
125 tty_unlock(tp);
126
127 return RT_EOK;
128
129 }
130
_setup_serial(struct rt_serial_device * serial,lwp_tty_t tp,struct serial_tty_context * softc)131 rt_inline void _setup_serial(struct rt_serial_device* serial, lwp_tty_t tp,
132 struct serial_tty_context *softc)
133 {
134 rt_bypass_lower_register(serial, "tty", RT_BYPASS_PROTECT_LEVEL_1, _serial_ty_bypass, (void *)tp);
135 }
136
_restore_serial(struct rt_serial_device * serial,lwp_tty_t tp,struct serial_tty_context * softc)137 rt_inline void _restore_serial(struct rt_serial_device *serial, lwp_tty_t tp,
138 struct serial_tty_context *softc)
139 {
140 rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &softc->backup_notify);
141 }
142
_serial_tty_set_speed(struct lwp_tty * tp)143 static void _serial_tty_set_speed(struct lwp_tty *tp)
144 {
145 struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
146 struct rt_serial_device *serial;
147 struct termios serial_hw_config;
148
149 RT_ASSERT(softc);
150 serial = softc->parent;
151
152 rt_device_control(&(serial->parent), TCGETS, &serial_hw_config);
153 tp->t_termios_init_in.c_cflag |= serial_hw_config.c_cflag;
154
155 tp->t_termios_init_in.__c_ispeed = tp->t_termios_init_in.__c_ospeed = cfgetospeed(&tp->t_termios_init_in);
156 }
157
_serial_isbusy(struct rt_serial_device * serial)158 static int _serial_isbusy(struct rt_serial_device *serial)
159 {
160 rt_thread_t user_thread = rt_console_current_user();
161 rt_thread_t self_thread = rt_thread_self();
162
163 return rt_console_get_device() == &serial->parent &&
164 (user_thread != RT_NULL && user_thread != self_thread);
165 }
166
serial_tty_outwakeup(struct lwp_tty * tp)167 static void serial_tty_outwakeup(struct lwp_tty *tp)
168 {
169 char out_char;
170 int len;
171 struct serial_tty_context *context = tty_softc(tp);
172 struct rt_serial_device *device;
173
174 if (!context || !context->parent)
175 {
176 LOG_E("%s: Data corruption", __func__);
177 return;
178 }
179
180 device = context->parent;
181
182 if (_serial_isbusy(device))
183 {
184 return ;
185 }
186
187 while ((len = ttydisc_getc(tp, &out_char, sizeof(out_char))) != 0)
188 {
189 device->ops->putc(device, out_char);
190
191 /* discard remaining if emergency output is happened */
192 if (_serial_isbusy(device))
193 {
194 break;
195 }
196 }
197 }
198
serial_tty_open(struct lwp_tty * tp)199 static int serial_tty_open(struct lwp_tty *tp)
200 {
201 struct serial_tty_context *softc;
202 struct rt_serial_device *serial;
203 rt_err_t error;
204 int oflags;
205
206 softc = tty_softc(tp);
207 serial = softc->parent;
208
209 LOG_D("%s", __func__);
210
211 rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
212
213 error = rt_device_open(&serial->parent, oflags);
214
215 if (!error)
216 {
217 /**
218 * to avoid driver accesssing null data,
219 * these are setup only after tty is registered
220 */
221 _setup_serial(serial, tp, softc);
222 }
223 return error;
224 }
225
serial_tty_close(struct lwp_tty * tp)226 static void serial_tty_close(struct lwp_tty *tp)
227 {
228 struct serial_tty_context *softc;
229 struct rt_serial_device *serial;
230 softc = tty_softc(tp);
231 serial = softc->parent;
232
233 LOG_D("%s", __func__);
234
235 rt_bypass_lower_unregister(serial, RT_BYPASS_PROTECT_LEVEL_1);
236 rt_device_close(&serial->parent);
237 }
238
serial_tty_ioctl(struct lwp_tty * tp,rt_ubase_t cmd,rt_caddr_t data,struct rt_thread * td)239 static int serial_tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
240 struct rt_thread *td)
241 {
242 int error;
243 switch (cmd)
244 {
245 default:
246 /**
247 * Note: for the most case, we don't let serial layer handle ioctl,
248 * for that they can't act properly regarding to the process
249 * management system, since it is unawared of that. So a ENOSYS is
250 * returned and caused the TTY layer to handle ioctl itself.
251 */
252 error = -ENOSYS;
253 break;
254 }
255 return error;
256 }
257
serial_tty_param(struct lwp_tty * tp,struct termios * t)258 static int serial_tty_param(struct lwp_tty *tp, struct termios *t)
259 {
260 struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
261 struct rt_serial_device *serial;
262
263 RT_ASSERT(softc);
264 serial = softc->parent;
265
266 if (!tty_opened(tp))
267 {
268 /**
269 * skip configure on open since all configs are copied from the current
270 * configuration on device. So we don't bother to set it back to device
271 * again.
272 */
273 return RT_EOK;
274 }
275
276 cfsetispeed(t, t->__c_ispeed);
277 return rt_device_control(&(serial->parent), TCSETS, t);
278 }
279
280 static struct lwp_ttydevsw serial_ttydevsw = {
281 .tsw_open = serial_tty_open,
282 .tsw_close = serial_tty_close,
283 .tsw_ioctl = serial_tty_ioctl,
284 .tsw_param = serial_tty_param,
285 .tsw_outwakeup = serial_tty_outwakeup,
286 };
287
rt_hw_serial_register_tty(struct rt_serial_device * serial)288 rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
289 {
290 rt_err_t rc;
291 lwp_tty_t tty;
292 char *dev_name;
293 struct serial_tty_context *softc;
294
295 if (serial->rx_notify.dev)
296 {
297 return -RT_EBUSY;
298 }
299
300 softc = rt_malloc(sizeof(struct serial_tty_context));
301 if (softc)
302 {
303 dev_name = alloc_device_name(serial);
304
305 if (dev_name)
306 {
307 softc->parent = serial;
308 tty = lwp_tty_create(&serial_ttydevsw, softc);
309 if (tty)
310 {
311 _serial_tty_set_speed(tty);
312 rc = lwp_tty_register(tty, dev_name);
313
314 if (rc != RT_EOK)
315 {
316 rt_free(tty);
317 rt_free(softc);
318 }
319 }
320 else
321 {
322 rt_free(softc);
323 rc = -RT_ENOMEM;
324 }
325
326 rt_free(dev_name);
327 }
328 else
329 {
330 rt_free(softc);
331 rc = -RT_ENOMEM;
332 }
333 }
334 else
335 {
336 rc = -RT_ENOMEM;
337 }
338
339 return rc;
340 }
341
rt_hw_serial_unregister_tty(struct rt_serial_device * serial)342 rt_err_t rt_hw_serial_unregister_tty(struct rt_serial_device *serial)
343 {
344 rt_device_t tty_dev;
345 lwp_tty_t tp;
346 struct serial_tty_context *softc;
347
348 tty_dev = serial->rx_notify.dev;
349 tp = rt_container_of(tty_dev, struct lwp_tty, parent);
350
351 /* restore serial setting */
352 softc = tty_softc(tp);
353 serial->rx_notify = softc->backup_notify;
354
355 tty_lock(tp);
356
357 tty_rel_gone(tp);
358
359 /* device unregister? */
360 rt_device_destroy(&tp->parent);
361 /* resource free? */
362 lwp_tty_delete(tp);
363
364 return RT_EOK;
365 }
366
_tty_workqueue_init(void)367 static int _tty_workqueue_init(void)
368 {
369 if (_ttyworkq != RT_NULL)
370 return RT_EOK;
371
372 _ttyworkq = rt_workqueue_create("ttyworkq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
373 LWP_TTY_WORKQUEUE_PRIORITY);
374 RT_ASSERT(_ttyworkq != RT_NULL);
375
376 _setup_debug_rxind_hook();
377
378 return RT_EOK;
379 }
380 INIT_PREV_EXPORT(_tty_workqueue_init);
381
_match_tty_iter(struct rt_object * obj,void * data)382 static rt_err_t _match_tty_iter(struct rt_object *obj, void *data)
383 {
384 rt_device_t target = *(rt_device_t *)data;
385 rt_device_t device = rt_container_of(obj, struct rt_device, parent);
386 if (device->type == RT_Device_Class_Char)
387 {
388 lwp_tty_t tp;
389 if (rt_strncmp(obj->name, "tty"TTY_NAME_PREFIX,
390 sizeof("tty"TTY_NAME_PREFIX) - 1) == 0)
391 {
392 struct serial_tty_context *softc;
393
394 tp = rt_container_of(device, struct lwp_tty, parent);
395 softc = tty_softc(tp);
396
397 if (&softc->parent->parent == target)
398 {
399 /* matched, early return */
400 *(rt_device_t *)data = device;
401 return 1;
402 }
403 }
404 }
405
406 return RT_EOK;
407 }
408
409 /**
410 * @brief The default console is only a backup device with lowest priority.
411 * It's always recommended to scratch the console from the boot arguments.
412 * And dont forget to register the device with a higher priority.
413 */
_default_console_setup(void)414 static int _default_console_setup(void)
415 {
416 rt_err_t rc;
417 rt_device_t bakdev;
418 rt_device_t ttydev;
419
420 bakdev = rt_console_get_device();
421 if (!bakdev)
422 {
423 return -RT_ENOENT;
424 }
425
426 ttydev = bakdev;
427 rt_object_for_each(RT_Object_Class_Device, _match_tty_iter, &ttydev);
428
429 if (ttydev != bakdev)
430 {
431 LOG_I("Using /dev/%.*s as default console", RT_NAME_MAX, ttydev->parent.name);
432 lwp_console_register_backend(ttydev, LWP_CONSOLE_LOWEST_PRIOR);
433 rc = RT_EOK;
434 }
435 else
436 {
437 rc = -RT_EINVAL;
438 }
439
440 return rc;
441 }
442
443 INIT_COMPONENT_EXPORT(_default_console_setup);
444