Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/drivers/include/drivers/dev_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ struct rt_serial_device
struct rt_serial_bypass* bypass;
#endif
struct rt_device_notify rx_notify;

#ifdef RT_USING_SMART
/* tty registered by rt_hw_serial_register_tty(), cleared on unregister */
struct rt_device *ttydev;
#endif
};
typedef struct rt_serial_device rt_serial_t;

Expand Down
5 changes: 5 additions & 0 deletions components/drivers/include/drivers/dev_serial_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ struct rt_serial_device
#ifdef RT_USING_POSIX_STDIO
rt_bool_t is_posix_mode;
#endif

#ifdef RT_USING_SMART
/* tty registered by rt_hw_serial_register_tty(), cleared on unregister */
struct rt_device *ttydev;
#endif
};

/**
Expand Down
191 changes: 174 additions & 17 deletions components/drivers/serial/serial_tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <sys/ioctl.h>

#define TTY_NAME_PREFIX "S" /* (S)erial */
#define TTY_NAME_PREFIX "S" /* (S)erial */
#define LWP_TTY_WORKQUEUE_PRIORITY 3

struct serial_tty_context
Expand All @@ -31,6 +31,18 @@ struct serial_tty_context

static struct rt_workqueue *_ttyworkq; /* system work queue */

#ifdef RT_USING_COMPONENTS_INIT
struct _tty_pending_registration
{
rt_slist_t node;
struct rt_serial_device *serial;
};

static rt_slist_t _tty_pending_registrations =
RT_SLIST_OBJECT_INIT(_tty_pending_registrations);
static rt_bool_t _tty_flush_done = RT_FALSE;
#endif /* RT_USING_COMPONENTS_INIT */

#ifndef RT_USING_DM
static rt_atomic_t _device_id_counter = 0;

Expand All @@ -40,13 +52,21 @@ static long get_dec_digits(rt_ubase_t val)
while (1)
{
if (val < 10)
{
return result;
}
if (val < 100)
{
return result + 1;
}
if (val < 1000)
{
return result + 2;
}
if (val < 10000)
{
return result + 3;
}
val /= 10000U;
result += 4;
}
Expand All @@ -69,13 +89,15 @@ static char *alloc_device_name(struct rt_serial_device *serial)
return RT_NULL;
}

long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
+ strlen(serial_name + sizeof("uart") - 1) /* suffix of serial device name*/
+ 1; /* tailing \0 */
+ 1; /* tailing \0 */

tty_dev_name = rt_malloc(digits_len);
if (tty_dev_name)
{
rt_sprintf(tty_dev_name, "%s%s", TTY_NAME_PREFIX, serial_name + sizeof("uart") - 1);
}
#else
RT_UNUSED(serial);
unsigned int devid = rt_atomic_add(&_device_id_counter, 1);
Expand All @@ -84,7 +106,9 @@ static char *alloc_device_name(struct rt_serial_device *serial)

tty_dev_name = rt_malloc(digits_len);
if (tty_dev_name)
{
rt_sprintf(tty_dev_name, "%s%u", TTY_NAME_PREFIX, devid);
}
#endif
return tty_dev_name;
}
Expand Down Expand Up @@ -115,12 +139,12 @@ int lwp_startup_debug_request(void)

static void _setup_debug_rxind_hook(void)
{
return ;
return;
}

#endif /* LWP_DEBUG_INIT */

static rt_err_t _serial_ty_bypass(struct rt_serial_device* serial, char ch,void *data)
static rt_err_t _serial_ty_bypass(struct rt_serial_device *serial, char ch, void *data)
{
lwp_tty_t tp;
tp = (lwp_tty_t)data;
Expand All @@ -131,10 +155,9 @@ static rt_err_t _serial_ty_bypass(struct rt_serial_device* serial, char ch,void
tty_unlock(tp);

return RT_EOK;

}

rt_inline void _setup_serial(struct rt_serial_device* serial, lwp_tty_t tp,
rt_inline void _setup_serial(struct rt_serial_device *serial, lwp_tty_t tp,
struct serial_tty_context *softc)
{
rt_bypass_lower_register(serial, "tty", RT_BYPASS_PROTECT_LEVEL_1, _serial_ty_bypass, (void *)tp);
Expand Down Expand Up @@ -200,7 +223,7 @@ static void serial_tty_outwakeup(struct lwp_tty *tp)

if (_serial_isbusy(device))
{
return ;
return;
}

while ((len = ttydisc_getc(tp, &out_char, sizeof(out_char))) != 0)
Expand Down Expand Up @@ -291,12 +314,22 @@ static int serial_tty_param(struct lwp_tty *tp, struct termios *t)
return rt_device_control(&(serial->parent), TCSETS, t);
}

static void serial_tty_free(void *softc)
{
/*
* lwp_tty_delete() calls the destructor of the tty device switch, both for
* a TTY that was opened as well as for one that never was.
*/
rt_free(softc);
}

static struct lwp_ttydevsw serial_ttydevsw = {
.tsw_open = serial_tty_open,
.tsw_close = serial_tty_close,
.tsw_ioctl = serial_tty_ioctl,
.tsw_param = serial_tty_param,
.tsw_outwakeup = serial_tty_outwakeup,
.tsw_free = serial_tty_free,
};

rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
Expand All @@ -306,11 +339,44 @@ rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
char *dev_name;
struct serial_tty_context *softc;

if (serial->rx_notify.dev)
if (serial->ttydev)
{
return -RT_EBUSY;
}

#ifdef RT_USING_COMPONENTS_INIT
/*
* Board code can register a serial device before INIT_PREV_EXPORT creates the
* tty workqueue.
*
* NOTE: The pending registrations list is accessed ONLY during the early
* initialization phase before the scheduler starts (single-threaded context).
* Once _tty_pending_flush has executed (_tty_flush_done == RT_TRUE), no further
* registrations may be queued onto the pending list; if the workqueue is still
* unavailable after flush, registration returns an error immediately.
*/
if (!_tty_flush_done && _ttyworkq == RT_NULL)
{
struct _tty_pending_registration *pending;

pending = rt_malloc(sizeof(*pending));
if (pending == RT_NULL)
{
return -RT_ENOMEM;
}

pending->serial = serial;
rt_slist_append(&_tty_pending_registrations, &pending->node);

return RT_EOK;
}
else if (_tty_flush_done && _ttyworkq == RT_NULL)
{
/* flush has completed but workqueue creation failed */
return -RT_ERROR;
}
#endif /* RT_USING_COMPONENTS_INIT */

softc = rt_malloc(sizeof(struct serial_tty_context));
if (softc)
{
Expand All @@ -330,6 +396,17 @@ rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
rt_free(tty);
rt_free(softc);
}
else
{
/*
* Keep the TTY here instead of relying on rx_notify.dev: the
* notify belongs to whoever uses it and is only installed when
* the TTY is opened, so it cannot tell "no TTY was ever
* created" from "the TTY exists but was never opened".
*/
softc->backup_notify = serial->rx_notify;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里在注册时保存 rx_notify,注销时又无条件写回。如果 TTY 一直没打开,期间其他调用方通过 RT_DEVICE_CTRL_NOTIFY_SET 设置了新的回调,注销 TTY 会把它覆盖掉。TTY 的收包走 bypass,请只撤销 TTY 自己安装的回调。

serial->ttydev = &tty->parent;
}
}
else
{
Expand Down Expand Up @@ -359,7 +436,44 @@ rt_err_t rt_hw_serial_unregister_tty(struct rt_serial_device *serial)
lwp_tty_t tp;
struct serial_tty_context *softc;

tty_dev = serial->rx_notify.dev;
#ifdef RT_USING_COMPONENTS_INIT
if (!_tty_flush_done)
{
rt_slist_t *node = _tty_pending_registrations.next;

while (node != RT_NULL)
{
struct _tty_pending_registration *pending;
rt_slist_t *next = node->next;

pending = rt_slist_entry(node, struct _tty_pending_registration, node);
if (pending->serial == serial)
{
rt_slist_remove(&_tty_pending_registrations, node);
rt_free(pending);
return RT_EOK;
}

node = next;
}
}
#endif /* RT_USING_COMPONENTS_INIT */

/*
* Registration may never have produced a TTY: the request could still be
* queued and be dropped by _tty_pending_flush(), the flush could fail, or
* TTY creation could have failed. rt_hw_serial_register_tty() reports
* RT_EOK for a queued request, so only a recorded TTY proves that one was
* created. A TTY that exists but was never opened must still be torn down;
* only the absence of the pointer means there is nothing to do.
*/
tty_dev = serial->ttydev;
if (tty_dev == RT_NULL)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TTY 注册成功后,首次打开时才会设置 rx_notify.dev。若从未打开就注销,这里会直接返回,留下已注册的设备节点。请保存注册成功的 TTY 指针,用它区分“未创建”和“已创建但未打开”。

{
return RT_EOK;
}
serial->ttydev = RT_NULL;

tp = rt_container_of(tty_dev, struct lwp_tty, parent);

/* restore serial setting */
Expand All @@ -368,20 +482,23 @@ rt_err_t rt_hw_serial_unregister_tty(struct rt_serial_device *serial)

tty_lock(tp);

/*
* tty_rel_gone() marks the TTY as gone and releases it: it deletes the TTY
* (device object, queues and our context through serial_tty_free()) once the
* last session has left, or right away when the TTY was never opened. The
* TTY must not be freed here as well.
*/
tty_rel_gone(tp);

/* device unregister? */
rt_device_destroy(&tp->parent);
/* resource free? */
lwp_tty_delete(tp);

return RT_EOK;
}

static int _tty_workqueue_init(void)
{
if (_ttyworkq != RT_NULL)
{
return RT_EOK;
}

_ttyworkq = rt_workqueue_create("ttyworkq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
LWP_TTY_WORKQUEUE_PRIORITY);
Expand All @@ -393,15 +510,55 @@ static int _tty_workqueue_init(void)
}
INIT_PREV_EXPORT(_tty_workqueue_init);

#ifdef RT_USING_COMPONENTS_INIT
static int _tty_pending_flush(void)
{
rt_slist_t *node;

_tty_flush_done = RT_TRUE;

if (_ttyworkq == RT_NULL)
{
LOG_E("tty workqueue is not available, cannot flush deferred registrations");
while ((node = rt_slist_pop(&_tty_pending_registrations)) != RT_NULL)
{
struct _tty_pending_registration *pending;

pending = rt_slist_entry(node, struct _tty_pending_registration, node);
rt_free(pending);
}
return -RT_ERROR;
}

while ((node = rt_slist_pop(&_tty_pending_registrations)) != RT_NULL)
{
struct _tty_pending_registration *pending;
rt_err_t rc;

pending = rt_slist_entry(node, struct _tty_pending_registration, node);
rc = rt_hw_serial_register_tty(pending->serial);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] flush 前确认工作队列已成功创建,避免将同一个注册请求反复入队

在关闭 RT_DEBUG 的配置中,_tty_workqueue_init() 的 RT_ASSERT(_ttyworkq != RT_NULL) 不会阻止 rt_workqueue_create() 分配失败后的启动流程,此时 _ttyworkq 仍为 NULL。这里调用 rt_hw_serial_register_tty() 会再次走 pending 分支:pop 一个节点、分配并 append 一个新节点、返回 RT_EOK,然后释放旧节点。只要剩余内存仍足够分配这个小节点(即使不足以创建工作队列线程栈),队列永远不为空,init 线程会在此持久循环,系统无法启动。

请在 flush 入口检测 _ttyworkq == RT_NULL 并返回明确错误,或把 flush 放到工作队列确认创建成功后的路径;不要在队列未就绪时重新调用会再次排队的注册入口。建议用工作队列分配失败、小 pending 分配仍成功的故障注入场景验证 flush 能及时返回。

if (rc != RT_EOK)
{
LOG_E("deferred tty registration failed: %d", rc);
}

rt_free(pending);
}

return RT_EOK;
}
INIT_COMPONENT_EXPORT(_tty_pending_flush);
#endif /* RT_USING_COMPONENTS_INIT */

static rt_err_t _match_tty_iter(struct rt_object *obj, void *data)
{
rt_device_t target = *(rt_device_t *)data;
rt_device_t device = rt_container_of(obj, struct rt_device, parent);
if (device->type == RT_Device_Class_Char)
{
lwp_tty_t tp;
if (rt_strncmp(obj->name, "tty"TTY_NAME_PREFIX,
sizeof("tty"TTY_NAME_PREFIX) - 1) == 0)
if (rt_strncmp(obj->name, "tty" TTY_NAME_PREFIX,
sizeof("tty" TTY_NAME_PREFIX) - 1) == 0)
{
struct serial_tty_context *softc;

Expand Down
Loading