add device check in register function, which is provided by Rob <rdent@iinet.net.au>.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2355 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong@gmail.com 2012-10-19 22:44:34 +00:00
parent 2bbd1496a3
commit e737514f86
1 changed files with 27 additions and 21 deletions

View File

@ -11,6 +11,8 @@
* Date Author Notes
* 2007-01-21 Bernard the first version
* 2010-05-04 Bernard add rt_device_init implementation
* 2012-10-20 Bernard add device check in register function,
* provided by Rob <rdent@iinet.net.au>
*/
#include <rtthread.h>
@ -31,6 +33,9 @@ rt_err_t rt_device_register(rt_device_t dev, const char *name, rt_uint16_t flags
if (dev == RT_NULL)
return -RT_ERROR;
if (rt_device_find(name) != RT_NULL)
return -RT_ERROR;
rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
dev->flag = flags;
@ -197,6 +202,8 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
/* if device is not initialized, initialize it. */
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
{
if (dev->init != RT_NULL )
{
result = dev->init(dev);
if (result != RT_EOK)
@ -206,10 +213,9 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
return result;
}
else
{
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
}
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
}
/* device is a stand alone device and opened */
@ -225,7 +231,7 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
else
{
/* no this interface in device driver */
result = -RT_ENOSYS;
/* result = -RT_ENOSYS; not set errno */
}
/* set open flag */
@ -259,7 +265,7 @@ rt_err_t rt_device_close(rt_device_t dev)
else
{
/* no this interface in device driver */
result = -RT_ENOSYS;
/* result = -RT_ENOSYS; not set errno */
}
/* set open flag */