[Kernel] Add device ops feature.

This commit is contained in:
Bernard Xiong 2018-06-10 17:59:17 +08:00
parent bca65f30a9
commit 884fb70fe9
37 changed files with 795 additions and 200 deletions

View File

@ -5,8 +5,8 @@
#include <rtthread.h> #include <rtthread.h>
#include "drv_clcd.h" #include "drv_clcd.h"
#define CLCD_WIDTH 480 #define CLCD_WIDTH 640
#define CLCD_HEIGHT 320 #define CLCD_HEIGHT 480
#define CLCD_DEVICE(dev) (struct drv_clcd_device*)(dev) #define CLCD_DEVICE(dev) (struct drv_clcd_device*)(dev)
@ -78,6 +78,18 @@ static rt_err_t drv_clcd_control(struct rt_device *device, int cmd, void *args)
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops clcd_ops =
{
drv_clcd_init,
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
drv_clcd_control
};
#endif
int drv_clcd_hw_init(void) int drv_clcd_hw_init(void)
{ {
PL111MMIO *plio; PL111MMIO *plio;
@ -107,8 +119,13 @@ int drv_clcd_hw_init(void)
plio->control = 0x1921 | (0x6 << 1); plio->control = 0x1921 | (0x6 << 1);
device->type = RT_Device_Class_Graphic; device->type = RT_Device_Class_Graphic;
#ifdef RT_USING_DEVICE_OPS
device->ops = &clcd_ops;
#else
device->init = drv_clcd_init; device->init = drv_clcd_init;
device->control = drv_clcd_control; device->control = drv_clcd_control;
#endif
rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR); rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
return 0; return 0;

View File

@ -485,6 +485,18 @@ struct pbuf *smc911x_emac_rx(rt_device_t dev)
return p; return p;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops smc911x_emac_ops =
{
smc911x_emac_init,
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
smc911x_emac_control
};
#endif
int smc911x_emac_hw_init(void) int smc911x_emac_hw_init(void)
{ {
_emac.iobase = VEXPRESS_ETH_BASE; _emac.iobase = VEXPRESS_ETH_BASE;
@ -507,12 +519,16 @@ int smc911x_emac_hw_init(void)
_emac.enetaddr[4] = 0x22; _emac.enetaddr[4] = 0x22;
_emac.enetaddr[5] = 0x33; _emac.enetaddr[5] = 0x33;
#ifdef RT_USING_DEVICE_OPS
_emac.parent.parent.ops = &smc911x_emac_ops;
#else
_emac.parent.parent.init = smc911x_emac_init; _emac.parent.parent.init = smc911x_emac_init;
_emac.parent.parent.open = RT_NULL; _emac.parent.parent.open = RT_NULL;
_emac.parent.parent.close = RT_NULL; _emac.parent.parent.close = RT_NULL;
_emac.parent.parent.read = RT_NULL; _emac.parent.parent.read = RT_NULL;
_emac.parent.parent.write = RT_NULL; _emac.parent.parent.write = RT_NULL;
_emac.parent.parent.control = smc911x_emac_control; _emac.parent.parent.control = smc911x_emac_control;
#endif
_emac.parent.parent.user_data = RT_NULL; _emac.parent.parent.user_data = RT_NULL;
_emac.parent.eth_rx = smc911x_emac_rx; _emac.parent.eth_rx = smc911x_emac_rx;
_emac.parent.eth_tx = smc911x_emac_tx; _emac.parent.eth_tx = smc911x_emac_tx;
@ -540,12 +556,3 @@ int smc911x_emac_hw_init(void)
return 0; return 0;
} }
INIT_APP_EXPORT(smc911x_emac_hw_init); INIT_APP_EXPORT(smc911x_emac_hw_init);
#include <finsh.h>
int emac(int argc, char** argv)
{
rt_hw_interrupt_umask(_emac.irqno);
return 0;
}
MSH_CMD_EXPORT(emac, emac dump);

View File

@ -421,6 +421,18 @@ static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args)
return result; return result;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops audio_ops =
{
_audio_dev_init,
_audio_dev_open,
_audio_dev_close,
_audio_dev_read,
_audio_dev_write,
_audio_dev_control
};
#endif
rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data) rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data)
{ {
struct rt_device *device; struct rt_device *device;
@ -431,12 +443,16 @@ rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_u
device->rx_indicate = RT_NULL; device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL; device->tx_complete = RT_NULL;
device->init = _audio_dev_init; #ifdef RT_USING_DEVICE_OPS
device->open = _audio_dev_open; device->ops = &audio_ops;
device->close = _audio_dev_close; #else
device->read = _audio_dev_read; device->init = _audio_dev_init;
device->write = _audio_dev_write; device->open = _audio_dev_open;
device->close = _audio_dev_close;
device->read = _audio_dev_read;
device->write = _audio_dev_write;
device->control = _audio_dev_control; device->control = _audio_dev_control;
#endif
device->user_data = data; device->user_data = data;
//init memory pool for replay //init memory pool for replay

View File

@ -193,14 +193,26 @@ static rt_size_t rt_pipe_write(rt_device_t dev,
static rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args) static rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
{ {
struct rt_audio_pipe *pipe; struct rt_audio_pipe *pipe;
pipe = (struct rt_audio_pipe *)dev; pipe = (struct rt_audio_pipe *)dev;
if (cmd == PIPE_CTRL_GET_SPACE && args) if (cmd == PIPE_CTRL_GET_SPACE && args)
*(rt_size_t*)args = rt_ringbuffer_space_len(&pipe->ringbuffer); *(rt_size_t*)args = rt_ringbuffer_space_len(&pipe->ringbuffer);
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops audio_pipe_ops
{
RT_NULL,
RT_NULL,
RT_NULL,
rt_pipe_read,
rt_pipe_write,
rt_pipe_control
};
#endif
/** /**
* This function will initialize a pipe device and put it under control of * This function will initialize a pipe device and put it under control of
* resource management. * resource management.
@ -233,12 +245,16 @@ rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe,
/* create pipe */ /* create pipe */
pipe->parent.type = RT_Device_Class_Pipe; pipe->parent.type = RT_Device_Class_Pipe;
#ifdef RT_USING_DEVICE_OPS
pipe->parent.ops = &audio_pipe_ops;
#else
pipe->parent.init = RT_NULL; pipe->parent.init = RT_NULL;
pipe->parent.open = RT_NULL; pipe->parent.open = RT_NULL;
pipe->parent.close = RT_NULL; pipe->parent.close = RT_NULL;
pipe->parent.read = rt_pipe_read; pipe->parent.read = rt_pipe_read;
pipe->parent.write = rt_pipe_write; pipe->parent.write = rt_pipe_write;
pipe->parent.control = rt_pipe_control; pipe->parent.control = rt_pipe_control;
#endif
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR); return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
} }

View File

@ -687,6 +687,18 @@ static void cantimeout(void *arg)
} }
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops can_device_ops =
{
rt_can_init,
rt_can_open,
rt_can_close,
rt_can_read,
rt_can_write,
rt_can_control
};
#endif
/* /*
* can register * can register
*/ */
@ -712,12 +724,17 @@ rt_err_t rt_hw_can_register(struct rt_can_device *can,
#ifdef RT_CAN_USING_BUS_HOOK #ifdef RT_CAN_USING_BUS_HOOK
can->bus_hook = RT_NULL; can->bus_hook = RT_NULL;
#endif /*RT_CAN_USING_BUS_HOOK*/ #endif /*RT_CAN_USING_BUS_HOOK*/
#ifdef RT_USING_DEVICE_OPS
device->ops = &can_device_ops;
#else
device->init = rt_can_init; device->init = rt_can_init;
device->open = rt_can_open; device->open = rt_can_open;
device->close = rt_can_close; device->close = rt_can_close;
device->read = rt_can_read; device->read = rt_can_read;
device->write = rt_can_write; device->write = rt_can_write;
device->control = rt_can_control; device->control = rt_can_control;
#endif
can->ops = ops; can->ops = ops;
can->status_indicate.ind = RT_NULL; can->status_indicate.ind = RT_NULL;

View File

@ -35,13 +35,13 @@ rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
float devi_min = 1; float devi_min = 1;
float devi; float devi;
/* 把定时器溢出时间和定时时间换算成秒 */ /* changed to second */
overflow = timer->info->maxcnt/(float)timer->freq; overflow = timer->info->maxcnt/(float)timer->freq;
tv_sec = tv->sec + tv->usec/(float)1000000; tv_sec = tv->sec + tv->usec/(float)1000000;
if (tv_sec < (1/(float)timer->freq)) if (tv_sec < (1/(float)timer->freq))
{ {
/* 定时时间小于计数周期 */ /* little timeout */
i = 0; i = 0;
timeout = 1/(float)timer->freq; timeout = 1/(float)timer->freq;
} }
@ -55,7 +55,7 @@ rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
{ {
counter = timeout*timer->freq; counter = timeout*timer->freq;
devi = tv_sec - (counter/(float)timer->freq)*i; devi = tv_sec - (counter/(float)timer->freq)*i;
/* 计算最小误差 */ /* Minimum calculation error */
if (devi > devi_min) if (devi > devi_min)
{ {
i = index; i = index;
@ -89,7 +89,7 @@ static rt_err_t rt_hwtimer_init(struct rt_device *dev)
rt_hwtimer_t *timer; rt_hwtimer_t *timer;
timer = (rt_hwtimer_t *)dev; timer = (rt_hwtimer_t *)dev;
/* 尝试将默认计数频率设为1Mhz */ /* try to change to 1MHz */
if ((1000000 <= timer->info->maxfreq) && (1000000 >= timer->info->minfreq)) if ((1000000 <= timer->info->maxfreq) && (1000000 >= timer->info->minfreq))
{ {
timer->freq = 1000000; timer->freq = 1000000;
@ -330,6 +330,18 @@ void rt_device_hwtimer_isr(rt_hwtimer_t *timer)
} }
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops hwtimer_ops =
{
rt_hwtimer_init,
rt_hwtimer_open,
rt_hwtimer_close,
rt_hwtimer_read,
rt_hwtimer_write,
rt_hwtimer_control
};
#endif
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data) rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data)
{ {
struct rt_device *device; struct rt_device *device;
@ -344,12 +356,16 @@ rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void
device->rx_indicate = RT_NULL; device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL; device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &hwtimer_ops;
#else
device->init = rt_hwtimer_init; device->init = rt_hwtimer_init;
device->open = rt_hwtimer_open; device->open = rt_hwtimer_open;
device->close = rt_hwtimer_close; device->close = rt_hwtimer_close;
device->read = rt_hwtimer_read; device->read = rt_hwtimer_read;
device->write = rt_hwtimer_write; device->write = rt_hwtimer_write;
device->control = rt_hwtimer_control; device->control = rt_hwtimer_control;
#endif
device->user_data = user_data; device->user_data = user_data;
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);

View File

@ -132,6 +132,18 @@ static rt_size_t fm24clxx_write(rt_device_t dev, rt_off_t pos, const void *buffe
return (ret == 2) ? size : 0; return (ret == 2) ? size : 0;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device fm24clxx_ops =
{
fm24clxx_init,
fm24clxx_open,
fm24clxx_close,
fm24clxx_read,
fm24clxx_write,
fm24clxx_control
};
#endif
rt_err_t fm24clxx_register(const char *fm_device_name, const char *i2c_bus, void *user_data) rt_err_t fm24clxx_register(const char *fm_device_name, const char *i2c_bus, void *user_data)
{ {
static struct fm24clxx_device fm24clxx_drv; static struct fm24clxx_device fm24clxx_drv;
@ -145,12 +157,17 @@ rt_err_t fm24clxx_register(const char *fm_device_name, const char *i2c_bus, void
fm24clxx_drv.bus = bus; fm24clxx_drv.bus = bus;
fm24clxx_drv.parent.type = RT_Device_Class_Block; fm24clxx_drv.parent.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
fm24clxx_drv.parent.ops = &fm24clxx_ops;
#else
fm24clxx_drv.parent.init = fm24clxx_init; fm24clxx_drv.parent.init = fm24clxx_init;
fm24clxx_drv.parent.open = fm24clxx_open; fm24clxx_drv.parent.open = fm24clxx_open;
fm24clxx_drv.parent.close = fm24clxx_close; fm24clxx_drv.parent.close = fm24clxx_close;
fm24clxx_drv.parent.read = fm24clxx_read; fm24clxx_drv.parent.read = fm24clxx_read;
fm24clxx_drv.parent.write = fm24clxx_write; fm24clxx_drv.parent.write = fm24clxx_write;
fm24clxx_drv.parent.control = fm24clxx_control; fm24clxx_drv.parent.control = fm24clxx_control;
#endif
fm24clxx_drv.parent.user_data = user_data; fm24clxx_drv.parent.user_data = user_data;
return rt_device_register(&fm24clxx_drv.parent, fm_device_name, RT_DEVICE_FLAG_RDWR); return rt_device_register(&fm24clxx_drv.parent, fm_device_name, RT_DEVICE_FLAG_RDWR);

View File

@ -102,6 +102,18 @@ static rt_err_t i2c_bus_device_control(rt_device_t dev,
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops i2c_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
i2c_bus_device_read,
i2c_bus_device_write,
i2c_bus_device_control
};
#endif
rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus, rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
const char *name) const char *name)
{ {
@ -115,12 +127,16 @@ rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
/* set device type */ /* set device type */
device->type = RT_Device_Class_I2CBUS; device->type = RT_Device_Class_I2CBUS;
/* initialize device interface */ /* initialize device interface */
#ifdef RT_USING_DEVICE_OPS
device->ops = &i2c_ops;
#else
device->init = RT_NULL; device->init = RT_NULL;
device->open = RT_NULL; device->open = RT_NULL;
device->close = RT_NULL; device->close = RT_NULL;
device->read = i2c_bus_device_read; device->read = i2c_bus_device_read;
device->write = i2c_bus_device_write; device->write = i2c_bus_device_write;
device->control = i2c_bus_device_control; device->control = i2c_bus_device_control;
#endif
/* register to device manager */ /* register to device manager */
rt_device_register(device, name, RT_DEVICE_FLAG_RDWR); rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);

View File

@ -76,18 +76,34 @@ static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args)
return 0; return 0;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops pin_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
_pin_read,
_pin_write,
_pin_control
};
#endif
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data) int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
{ {
_hw_pin.parent.type = RT_Device_Class_Miscellaneous; _hw_pin.parent.type = RT_Device_Class_Miscellaneous;
_hw_pin.parent.rx_indicate = RT_NULL; _hw_pin.parent.rx_indicate = RT_NULL;
_hw_pin.parent.tx_complete = RT_NULL; _hw_pin.parent.tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
_hw_pin.parent.ops = &pin_ops;
#else
_hw_pin.parent.init = RT_NULL; _hw_pin.parent.init = RT_NULL;
_hw_pin.parent.open = RT_NULL; _hw_pin.parent.open = RT_NULL;
_hw_pin.parent.close = RT_NULL; _hw_pin.parent.close = RT_NULL;
_hw_pin.parent.read = _pin_read; _hw_pin.parent.read = _pin_read;
_hw_pin.parent.write = _pin_write; _hw_pin.parent.write = _pin_write;
_hw_pin.parent.control = _pin_control; _hw_pin.parent.control = _pin_control;
#endif
_hw_pin.ops = ops; _hw_pin.ops = ops;
_hw_pin.parent.user_data = user_data; _hw_pin.parent.user_data = user_data;
@ -117,6 +133,7 @@ rt_err_t rt_pin_dettach_irq(rt_int32_t pin)
} }
return RT_ENOSYS; return RT_ENOSYS;
} }
rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled) rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
{ {
RT_ASSERT(_hw_pin.ops != RT_NULL); RT_ASSERT(_hw_pin.ops != RT_NULL);
@ -126,6 +143,7 @@ rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
} }
return RT_ENOSYS; return RT_ENOSYS;
} }
/* RT-Thread Hardware PIN APIs */ /* RT-Thread Hardware PIN APIs */
void rt_pin_mode(rt_base_t pin, rt_base_t mode) void rt_pin_mode(rt_base_t pin, rt_base_t mode)
{ {

View File

@ -69,6 +69,18 @@ static rt_err_t _mtd_control(rt_device_t dev, int cmd, void *args)
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops mtd_nand_ops =
{
_mtd_init,
_mtd_open,
_mtd_close,
_mtd_read,
_mtd_write,
_mtd_control
};
#endif
rt_err_t rt_mtd_nand_register_device(const char *name, rt_err_t rt_mtd_nand_register_device(const char *name,
struct rt_mtd_nand_device *device) struct rt_mtd_nand_device *device)
{ {
@ -79,12 +91,16 @@ rt_err_t rt_mtd_nand_register_device(const char *name,
/* set device class and generic device interface */ /* set device class and generic device interface */
dev->type = RT_Device_Class_MTD; dev->type = RT_Device_Class_MTD;
#ifdef RT_USING_DEVICE_OPS
dev->ops = &mtd_nand_ops;
#else
dev->init = _mtd_init; dev->init = _mtd_init;
dev->open = _mtd_open; dev->open = _mtd_open;
dev->read = _mtd_read; dev->read = _mtd_read;
dev->write = _mtd_write; dev->write = _mtd_write;
dev->close = _mtd_close; dev->close = _mtd_close;
dev->control = _mtd_control; dev->control = _mtd_control;
#endif
dev->rx_indicate = RT_NULL; dev->rx_indicate = RT_NULL;
dev->tx_complete = RT_NULL; dev->tx_complete = RT_NULL;

View File

@ -65,6 +65,18 @@ static rt_err_t _mtd_control(rt_device_t dev, int cmd, void *args)
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops mtd_nor_ops =
{
_mtd_init,
_mtd_open,
_mtd_close,
_mtd_read,
_mtd_write,
_mtd_control
};
#endif
rt_err_t rt_mtd_nor_register_device(const char *name, rt_err_t rt_mtd_nor_register_device(const char *name,
struct rt_mtd_nor_device *device) struct rt_mtd_nor_device *device)
{ {
@ -75,12 +87,16 @@ rt_err_t rt_mtd_nor_register_device(const char *name,
/* set device class and generic device interface */ /* set device class and generic device interface */
dev->type = RT_Device_Class_MTD; dev->type = RT_Device_Class_MTD;
#ifdef RT_USING_DEVICE_OPS
dev->ops = &mtd_nor_ops;
#else
dev->init = _mtd_init; dev->init = _mtd_init;
dev->open = _mtd_open; dev->open = _mtd_open;
dev->read = _mtd_read; dev->read = _mtd_read;
dev->write = _mtd_write; dev->write = _mtd_write;
dev->close = _mtd_close; dev->close = _mtd_close;
dev->control = _mtd_control; dev->control = _mtd_control;
#endif
dev->rx_indicate = RT_NULL; dev->rx_indicate = RT_NULL;
dev->tx_complete = RT_NULL; dev->tx_complete = RT_NULL;

View File

@ -68,6 +68,18 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops soft_rtc_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
soft_rtc_control
};
#endif
int rt_soft_rtc_init(void) int rt_soft_rtc_init(void)
{ {
static rt_bool_t init_ok = RT_FALSE; static rt_bool_t init_ok = RT_FALSE;
@ -86,12 +98,16 @@ int rt_soft_rtc_init(void)
soft_rtc_dev.type = RT_Device_Class_RTC; soft_rtc_dev.type = RT_Device_Class_RTC;
/* register rtc device */ /* register rtc device */
#ifdef RT_USING_DEVICE_OPS
soft_rtc_dev.ops = &soft_rtc_ops;
#else
soft_rtc_dev.init = RT_NULL; soft_rtc_dev.init = RT_NULL;
soft_rtc_dev.open = RT_NULL; soft_rtc_dev.open = RT_NULL;
soft_rtc_dev.close = RT_NULL; soft_rtc_dev.close = RT_NULL;
soft_rtc_dev.read = RT_NULL; soft_rtc_dev.read = RT_NULL;
soft_rtc_dev.write = RT_NULL; soft_rtc_dev.write = RT_NULL;
soft_rtc_dev.control = soft_rtc_control; soft_rtc_dev.control = soft_rtc_control;
#endif
/* no private */ /* no private */
soft_rtc_dev.user_data = RT_NULL; soft_rtc_dev.user_data = RT_NULL;

View File

@ -319,6 +319,18 @@ static rt_int32_t mmcsd_set_blksize(struct rt_mmcsd_card *card)
return 0; return 0;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops mmcsd_blk_ops =
{
rt_mmcsd_init,
rt_mmcsd_open,
rt_mmcsd_close,
rt_mmcsd_read,
rt_mmcsd_write,
rt_mmcsd_control
};
#endif
rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card) rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)
{ {
rt_int32_t err = 0; rt_int32_t err = 0;
@ -366,13 +378,17 @@ rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)
blk_dev->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO); blk_dev->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
/* register mmcsd device */ /* register mmcsd device */
blk_dev->dev.type = RT_Device_Class_Block; blk_dev->dev.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
blk_dev->dev.ops = &mmcsd_blk_ops;
#else
blk_dev->dev.init = rt_mmcsd_init; blk_dev->dev.init = rt_mmcsd_init;
blk_dev->dev.open = rt_mmcsd_open; blk_dev->dev.open = rt_mmcsd_open;
blk_dev->dev.close = rt_mmcsd_close; blk_dev->dev.close = rt_mmcsd_close;
blk_dev->dev.read = rt_mmcsd_read; blk_dev->dev.read = rt_mmcsd_read;
blk_dev->dev.write = rt_mmcsd_write; blk_dev->dev.write = rt_mmcsd_write;
blk_dev->dev.control = rt_mmcsd_control; blk_dev->dev.control = rt_mmcsd_control;
#endif
blk_dev->dev.user_data = blk_dev; blk_dev->dev.user_data = blk_dev;
blk_dev->card = card; blk_dev->card = card;
@ -396,12 +412,16 @@ rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)
/* register mmcsd device */ /* register mmcsd device */
blk_dev->dev.type = RT_Device_Class_Block; blk_dev->dev.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
blk_dev->dev.ops = &mmcsd_blk_ops;
#else
blk_dev->dev.init = rt_mmcsd_init; blk_dev->dev.init = rt_mmcsd_init;
blk_dev->dev.open = rt_mmcsd_open; blk_dev->dev.open = rt_mmcsd_open;
blk_dev->dev.close = rt_mmcsd_close; blk_dev->dev.close = rt_mmcsd_close;
blk_dev->dev.read = rt_mmcsd_read; blk_dev->dev.read = rt_mmcsd_read;
blk_dev->dev.write = rt_mmcsd_write; blk_dev->dev.write = rt_mmcsd_write;
blk_dev->dev.control = rt_mmcsd_control; blk_dev->dev.control = rt_mmcsd_control;
#endif
blk_dev->dev.user_data = blk_dev; blk_dev->dev.user_data = blk_dev;
blk_dev->card = card; blk_dev->card = card;
@ -482,6 +502,3 @@ int rt_mmcsd_blk_init(void)
/* nothing */ /* nothing */
return 0; return 0;
} }
INIT_PREV_EXPORT(rt_mmcsd_blk_init);

View File

@ -1072,6 +1072,18 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
return ret; return ret;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops serial_ops =
{
rt_serial_init,
rt_serial_open,
rt_serial_close,
rt_serial_read,
rt_serial_write,
rt_serial_control
};
#endif
/* /*
* serial register * serial register
*/ */
@ -1090,12 +1102,16 @@ rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
device->rx_indicate = RT_NULL; device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL; device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &serial_ops;
#else
device->init = rt_serial_init; device->init = rt_serial_init;
device->open = rt_serial_open; device->open = rt_serial_open;
device->close = rt_serial_close; device->close = rt_serial_close;
device->read = rt_serial_read; device->read = rt_serial_read;
device->write = rt_serial_write; device->write = rt_serial_write;
device->control = rt_serial_control; device->control = rt_serial_control;
#endif
device->user_data = data; device->user_data = data;
/* register a character device */ /* register a character device */

View File

@ -741,6 +741,18 @@ static struct pbuf *enc28j60_rx(rt_device_t dev)
return p; return p;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops enc28j60_ops =
{
enc28j60_init,
enc28j60_open,
enc28j60_close,
enc28j60_read,
enc28j60_write,
enc28j60_control
};
#endif
rt_err_t enc28j60_attach(const char *spi_device_name) rt_err_t enc28j60_attach(const char *spi_device_name)
{ {
struct rt_spi_device *spi_device; struct rt_spi_device *spi_device;
@ -802,12 +814,16 @@ rt_err_t enc28j60_attach(const char *spi_device_name)
/* init rt-thread device struct */ /* init rt-thread device struct */
enc28j60_dev.parent.parent.type = RT_Device_Class_NetIf; enc28j60_dev.parent.parent.type = RT_Device_Class_NetIf;
#ifdef RT_USING_DEVICE_OPS
enc28j60_dev.parent.parent.ops = &enc28j60_ops;
#else
enc28j60_dev.parent.parent.init = enc28j60_init; enc28j60_dev.parent.parent.init = enc28j60_init;
enc28j60_dev.parent.parent.open = enc28j60_open; enc28j60_dev.parent.parent.open = enc28j60_open;
enc28j60_dev.parent.parent.close = enc28j60_close; enc28j60_dev.parent.parent.close = enc28j60_close;
enc28j60_dev.parent.parent.read = enc28j60_read; enc28j60_dev.parent.parent.read = enc28j60_read;
enc28j60_dev.parent.parent.write = enc28j60_write; enc28j60_dev.parent.parent.write = enc28j60_write;
enc28j60_dev.parent.parent.control = enc28j60_control; enc28j60_dev.parent.parent.control = enc28j60_control;
#endif
/* init rt-thread ethernet device struct */ /* init rt-thread ethernet device struct */
enc28j60_dev.parent.eth_rx = enc28j60_rx; enc28j60_dev.parent.eth_rx = enc28j60_rx;

View File

@ -69,6 +69,18 @@ static rt_err_t _spi_bus_device_control(rt_device_t dev,
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops spi_bus_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
_spi_bus_device_read,
_spi_bus_device_write,
_spi_bus_device_control
};
#endif
rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name) rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name)
{ {
struct rt_device *device; struct rt_device *device;
@ -79,12 +91,16 @@ rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name)
/* set device type */ /* set device type */
device->type = RT_Device_Class_SPIBUS; device->type = RT_Device_Class_SPIBUS;
/* initialize device interface */ /* initialize device interface */
#ifdef RT_USING_DEVICE_OPS
device->ops = &spi_bus_ops;
#else
device->init = RT_NULL; device->init = RT_NULL;
device->open = RT_NULL; device->open = RT_NULL;
device->close = RT_NULL; device->close = RT_NULL;
device->read = _spi_bus_device_read; device->read = _spi_bus_device_read;
device->write = _spi_bus_device_write; device->write = _spi_bus_device_write;
device->control = _spi_bus_device_control; device->control = _spi_bus_device_control;
#endif
/* register to device manager */ /* register to device manager */
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR); return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
@ -134,6 +150,18 @@ static rt_err_t _spidev_device_control(rt_device_t dev,
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops spi_device_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
_spidev_device_read,
_spidev_device_write,
_spidev_device_control
};
#endif
rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name) rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
{ {
struct rt_device *device; struct rt_device *device;
@ -143,13 +171,17 @@ rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
/* set device type */ /* set device type */
device->type = RT_Device_Class_SPIDevice; device->type = RT_Device_Class_SPIDevice;
#ifdef RT_USING_DEVICE_OPS
device->ops = &spi_device_ops;
#else
device->init = RT_NULL; device->init = RT_NULL;
device->open = RT_NULL; device->open = RT_NULL;
device->close = RT_NULL; device->close = RT_NULL;
device->read = _spidev_device_read; device->read = _spidev_device_read;
device->write = _spidev_device_write; device->write = _spidev_device_write;
device->control = _spidev_device_control; device->control = _spidev_device_control;
#endif
/* register to device manager */ /* register to device manager */
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR); return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
} }

View File

@ -1,7 +1,7 @@
/* /*
* File : spi_flash_gd.c * File : spi_flash_gd.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
* All rights reserved * All rights reserved
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -254,6 +254,18 @@ static rt_size_t w25qxx_flash_write(rt_device_t dev,
return size; return size;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops gd_device_ops =
{
w25qxx_flash_init,
w25qxx_flash_open,
w25qxx_flash_close,
w25qxx_flash_read,
w25qxx_flash_write,
w25qxx_flash_control
};
#endif
rt_err_t gd_init(const char * flash_device_name, const char * spi_device_name) rt_err_t gd_init(const char * flash_device_name, const char * spi_device_name)
{ {
struct rt_spi_device * rt_spi_device; struct rt_spi_device * rt_spi_device;
@ -330,12 +342,16 @@ rt_err_t gd_init(const char * flash_device_name, const char * spi_device_name)
/* register device */ /* register device */
spi_flash_device.flash_device.type = RT_Device_Class_Block; spi_flash_device.flash_device.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
spi_flash_device.flash_device.ops = &gd_device_ops;
#else
spi_flash_device.flash_device.init = w25qxx_flash_init; spi_flash_device.flash_device.init = w25qxx_flash_init;
spi_flash_device.flash_device.open = w25qxx_flash_open; spi_flash_device.flash_device.open = w25qxx_flash_open;
spi_flash_device.flash_device.close = w25qxx_flash_close; spi_flash_device.flash_device.close = w25qxx_flash_close;
spi_flash_device.flash_device.read = w25qxx_flash_read; spi_flash_device.flash_device.read = w25qxx_flash_read;
spi_flash_device.flash_device.write = w25qxx_flash_write; spi_flash_device.flash_device.write = w25qxx_flash_write;
spi_flash_device.flash_device.control = w25qxx_flash_control; spi_flash_device.flash_device.control = w25qxx_flash_control;
#endif
/* no private */ /* no private */
spi_flash_device.flash_device.user_data = RT_NULL; spi_flash_device.flash_device.user_data = RT_NULL;

View File

@ -225,6 +225,18 @@ sfud_err sfud_spi_port_init(sfud_flash *flash) {
return result; return result;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops flash_device_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
rt_sfud_read,
rt_sfud_write,
rt_sfud_control
};
#endif
/** /**
* Probe SPI flash by SFUD(Serial Flash Universal Driver) driver library and though SPI device. * Probe SPI flash by SFUD(Serial Flash Universal Driver) driver library and though SPI device.
* *
@ -255,7 +267,7 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
/* initialize lock */ /* initialize lock */
rt_mutex_init(&(rtt_dev->lock), spi_flash_dev_name, RT_IPC_FLAG_FIFO); rt_mutex_init(&(rtt_dev->lock), spi_flash_dev_name, RT_IPC_FLAG_FIFO);
} }
if (rtt_dev && sfud_dev && spi_flash_dev_name_bak && spi_dev_name_bak) { if (rtt_dev && sfud_dev && spi_flash_dev_name_bak && spi_dev_name_bak) {
rt_memset(sfud_dev, 0, sizeof(sfud_flash)); rt_memset(sfud_dev, 0, sizeof(sfud_flash));
rt_strncpy(spi_flash_dev_name_bak, spi_flash_dev_name, rt_strlen(spi_flash_dev_name)); rt_strncpy(spi_flash_dev_name_bak, spi_flash_dev_name, rt_strlen(spi_flash_dev_name));
@ -294,12 +306,16 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
/* register device */ /* register device */
rtt_dev->flash_device.type = RT_Device_Class_Block; rtt_dev->flash_device.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
rtt_dev->flash_device.ops = &flash_device_ops;
#else
rtt_dev->flash_device.init = RT_NULL; rtt_dev->flash_device.init = RT_NULL;
rtt_dev->flash_device.open = RT_NULL; rtt_dev->flash_device.open = RT_NULL;
rtt_dev->flash_device.close = RT_NULL; rtt_dev->flash_device.close = RT_NULL;
rtt_dev->flash_device.read = rt_sfud_read; rtt_dev->flash_device.read = rt_sfud_read;
rtt_dev->flash_device.write = rt_sfud_write; rtt_dev->flash_device.write = rt_sfud_write;
rtt_dev->flash_device.control = rt_sfud_control; rtt_dev->flash_device.control = rt_sfud_control;
#endif
rt_device_register(&(rtt_dev->flash_device), spi_flash_dev_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); rt_device_register(&(rtt_dev->flash_device), spi_flash_dev_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);

View File

@ -254,6 +254,18 @@ static rt_size_t sst25vfxx_flash_write(rt_device_t dev, rt_off_t pos, const void
return size; return size;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops sst25vfxx_device_ops =
{
sst25vfxx_flash_init,
sst25vfxx_flash_open,
sst25vfxx_flash_close,
sst25vfxx_flash_read,
sst25vfxx_flash_write,
sst25vfxx_flash_control
};
#endif
rt_err_t sst25vfxx_init(const char * flash_device_name, const char * spi_device_name) rt_err_t sst25vfxx_init(const char * flash_device_name, const char * spi_device_name)
{ {
struct rt_spi_device * rt_spi_device; struct rt_spi_device * rt_spi_device;
@ -340,12 +352,16 @@ rt_err_t sst25vfxx_init(const char * flash_device_name, const char * spi_device_
/* register device */ /* register device */
spi_flash->flash_device.type = RT_Device_Class_Block; spi_flash->flash_device.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
spi_flash->flash_device.ops = &sst25vfxx_device_ops;
#else
spi_flash->flash_device.init = sst25vfxx_flash_init; spi_flash->flash_device.init = sst25vfxx_flash_init;
spi_flash->flash_device.open = sst25vfxx_flash_open; spi_flash->flash_device.open = sst25vfxx_flash_open;
spi_flash->flash_device.close = sst25vfxx_flash_close; spi_flash->flash_device.close = sst25vfxx_flash_close;
spi_flash->flash_device.read = sst25vfxx_flash_read; spi_flash->flash_device.read = sst25vfxx_flash_read;
spi_flash->flash_device.write = sst25vfxx_flash_write; spi_flash->flash_device.write = sst25vfxx_flash_write;
spi_flash->flash_device.control = sst25vfxx_flash_control; spi_flash->flash_device.control = sst25vfxx_flash_control;
#endif
/* no private */ /* no private */
spi_flash->flash_device.user_data = RT_NULL; spi_flash->flash_device.user_data = RT_NULL;

View File

@ -264,6 +264,18 @@ static rt_size_t w25qxx_flash_write(rt_device_t dev,
return size; return size;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops w25qxx_device_ops =
{
w25qxx_flash_init,
w25qxx_flash_open,
w25qxx_flash_close,
w25qxx_flash_read,
w25qxx_flash_write,
w25qxx_flash_control
};
#endif
rt_err_t w25qxx_init(const char * flash_device_name, const char * spi_device_name) rt_err_t w25qxx_init(const char * flash_device_name, const char * spi_device_name)
{ {
struct rt_spi_device * rt_spi_device; struct rt_spi_device * rt_spi_device;
@ -375,12 +387,16 @@ rt_err_t w25qxx_init(const char * flash_device_name, const char * spi_device_nam
/* register device */ /* register device */
spi_flash_device.flash_device.type = RT_Device_Class_Block; spi_flash_device.flash_device.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
spi_flash_device.flash_device.ops = &w25qxx_device_ops;
#else
spi_flash_device.flash_device.init = w25qxx_flash_init; spi_flash_device.flash_device.init = w25qxx_flash_init;
spi_flash_device.flash_device.open = w25qxx_flash_open; spi_flash_device.flash_device.open = w25qxx_flash_open;
spi_flash_device.flash_device.close = w25qxx_flash_close; spi_flash_device.flash_device.close = w25qxx_flash_close;
spi_flash_device.flash_device.read = w25qxx_flash_read; spi_flash_device.flash_device.read = w25qxx_flash_read;
spi_flash_device.flash_device.write = w25qxx_flash_write; spi_flash_device.flash_device.write = w25qxx_flash_write;
spi_flash_device.flash_device.control = w25qxx_flash_control; spi_flash_device.flash_device.control = w25qxx_flash_control;
#endif
/* no private */ /* no private */
spi_flash_device.flash_device.user_data = RT_NULL; spi_flash_device.flash_device.user_data = RT_NULL;

View File

@ -47,10 +47,14 @@ static void MSD_release_cs(struct rt_spi_device *device);
static rt_err_t _wait_token(struct rt_spi_device *device, uint8_t token); static rt_err_t _wait_token(struct rt_spi_device *device, uint8_t token);
static rt_err_t _wait_ready(struct rt_spi_device *device); static rt_err_t _wait_ready(struct rt_spi_device *device);
static rt_err_t rt_msd_init(rt_device_t dev);
static rt_err_t rt_msd_open(rt_device_t dev, rt_uint16_t oflag);
static rt_err_t rt_msd_close(rt_device_t dev);
static rt_size_t rt_msd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); static rt_size_t rt_msd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
static rt_size_t rt_msd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size); static rt_size_t rt_msd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
static rt_size_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size); static rt_size_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
static rt_size_t rt_msd_sdhc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); static rt_size_t rt_msd_sdhc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
static rt_err_t rt_msd_control(rt_device_t dev, int cmd, void *args);
static rt_err_t MSD_take_owner(struct rt_spi_device *spi_device) static rt_err_t MSD_take_owner(struct rt_spi_device *spi_device)
{ {
@ -461,6 +465,28 @@ static rt_err_t _write_block(struct rt_spi_device *device, const void *buffer, u
return _wait_ready(device); return _wait_ready(device);
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops msd_ops =
{
rt_msd_init,
rt_msd_open,
rt_msd_close,
rt_msd_read,
rt_msd_write,
rt_msd_control
};
const static struct rt_device_ops msd_sdhc_ops =
{
rt_msd_init,
rt_msd_open,
rt_msd_close,
rt_msd_sdhc_read,
rt_msd_sdhc_write,
rt_msd_control
};
#endif
/* RT-Thread Device Driver Interface */ /* RT-Thread Device Driver Interface */
static rt_err_t rt_msd_init(rt_device_t dev) static rt_err_t rt_msd_init(rt_device_t dev)
{ {
@ -893,13 +919,21 @@ static rt_err_t rt_msd_init(rt_device_t dev)
if (msd->card_type == MSD_CARD_TYPE_SD_SDHC) if (msd->card_type == MSD_CARD_TYPE_SD_SDHC)
{ {
#ifdef RT_USING_DEVICE_OPS
dev->ops = &msd_sdhc_ops;
#else
dev->read = rt_msd_sdhc_read; dev->read = rt_msd_sdhc_read;
dev->write = rt_msd_sdhc_write; dev->write = rt_msd_sdhc_write;
#endif
} }
else else
{ {
#ifdef RT_USING_DEVICE_OPS
dev->ops = &msd_ops;
#else
dev->read = rt_msd_read; dev->read = rt_msd_read;
dev->write = rt_msd_write; dev->write = rt_msd_write;
#endif
} }
/* set CRC */ /* set CRC */
@ -1674,12 +1708,16 @@ rt_err_t msd_init(const char *sd_device_name, const char *spi_device_name)
_msd_device.geometry.sector_count = 0; _msd_device.geometry.sector_count = 0;
_msd_device.geometry.block_size = 0; _msd_device.geometry.block_size = 0;
#ifdef RT_USING_DEVICE_OPS
_msd_device.parent.ops = &msd_ops;
#else
_msd_device.parent.init = rt_msd_init; _msd_device.parent.init = rt_msd_init;
_msd_device.parent.open = rt_msd_open; _msd_device.parent.open = rt_msd_open;
_msd_device.parent.close = rt_msd_close; _msd_device.parent.close = rt_msd_close;
_msd_device.parent.read = RT_NULL; _msd_device.parent.read = RT_NULL;
_msd_device.parent.write = RT_NULL; _msd_device.parent.write = RT_NULL;
_msd_device.parent.control = rt_msd_control; _msd_device.parent.control = rt_msd_control;
#endif
/* no private, no callback */ /* no private, no callback */
_msd_device.parent.user_data = RT_NULL; _msd_device.parent.user_data = RT_NULL;

View File

@ -629,6 +629,18 @@ static void spi_wifi_data_thread_entry(void *parameter)
} }
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops rw009_ops =
{
rw009_wifi_init,
rw009_wifi_open,
rw009_wifi_close,
rw009_wifi_read,
rw009_wifi_write,
rw009_wifi_control
};
#endif
rt_err_t rt_hw_wifi_init(const char *spi_device_name, wifi_mode_t mode) rt_err_t rt_hw_wifi_init(const char *spi_device_name, wifi_mode_t mode)
{ {
/* align and struct size check. */ /* align and struct size check. */
@ -654,12 +666,16 @@ rt_err_t rt_hw_wifi_init(const char *spi_device_name, wifi_mode_t mode)
rt_spi_configure(rw009_wifi_device.rt_spi_device, &cfg); rt_spi_configure(rw009_wifi_device.rt_spi_device, &cfg);
} }
#ifdef RT_USING_DEVICE_OPS
rw009_wifi_device.parent.parent.ops = &rw009_ops;
#else
rw009_wifi_device.parent.parent.init = rw009_wifi_init; rw009_wifi_device.parent.parent.init = rw009_wifi_init;
rw009_wifi_device.parent.parent.open = rw009_wifi_open; rw009_wifi_device.parent.parent.open = rw009_wifi_open;
rw009_wifi_device.parent.parent.close = rw009_wifi_close; rw009_wifi_device.parent.parent.close = rw009_wifi_close;
rw009_wifi_device.parent.parent.read = rw009_wifi_read; rw009_wifi_device.parent.parent.read = rw009_wifi_read;
rw009_wifi_device.parent.parent.write = rw009_wifi_write; rw009_wifi_device.parent.parent.write = rw009_wifi_write;
rw009_wifi_device.parent.parent.control = rw009_wifi_control; rw009_wifi_device.parent.parent.control = rw009_wifi_control;
#endif
rw009_wifi_device.parent.parent.user_data = RT_NULL; rw009_wifi_device.parent.parent.user_data = RT_NULL;
rw009_wifi_device.parent.eth_rx = rw009_wifi_rx; rw009_wifi_device.parent.eth_rx = rw009_wifi_rx;

View File

@ -416,6 +416,18 @@ rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops pipe_ops =
{
RT_NULL,
rt_pipe_open,
rt_pipe_close,
rt_pipe_read,
rt_pipe_write,
rt_pipe_control,
};
#endif
rt_pipe_t *rt_pipe_create(const char *name, int bufsz) rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
{ {
rt_pipe_t *pipe; rt_pipe_t *pipe;
@ -434,12 +446,16 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
dev = &(pipe->parent); dev = &(pipe->parent);
dev->type = RT_Device_Class_Pipe; dev->type = RT_Device_Class_Pipe;
#ifdef RT_USING_DEVICE_OPS
dev->ops = &pipe_ops;
#else
dev->init = RT_NULL; dev->init = RT_NULL;
dev->open = rt_pipe_open; dev->open = rt_pipe_open;
dev->read = rt_pipe_read; dev->read = rt_pipe_read;
dev->write = rt_pipe_write; dev->write = rt_pipe_write;
dev->close = rt_pipe_close; dev->close = rt_pipe_close;
dev->control = rt_pipe_control; dev->control = rt_pipe_control;
#endif
dev->rx_indicate = RT_NULL; dev->rx_indicate = RT_NULL;
dev->tx_complete = RT_NULL; dev->tx_complete = RT_NULL;

View File

@ -327,6 +327,19 @@ static rt_err_t rt_ecm_eth_control(rt_device_t dev, int cmd, void *args)
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops ecm_device_ops =
{
rt_ecm_eth_init,
rt_ecm_eth_open,
rt_ecm_eth_close,
rt_ecm_eth_read,
rt_ecm_eth_write,
rt_ecm_eth_control
};
#endif
struct pbuf *rt_ecm_eth_rx(rt_device_t dev) struct pbuf *rt_ecm_eth_rx(rt_device_t dev)
{ {
struct pbuf* p = RT_NULL; struct pbuf* p = RT_NULL;

View File

@ -592,14 +592,33 @@ static void hid_thread_entry(void* parameter)
HID_Report_Received(&report); HID_Report_Received(&report);
} }
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops hid_device_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
_hid_write,
RT_NULL,
};
#endif
static rt_uint8_t hid_mq_pool[(sizeof(struct hid_report)+sizeof(void*))*8]; static rt_uint8_t hid_mq_pool[(sizeof(struct hid_report)+sizeof(void*))*8];
static void rt_usb_hid_init(struct ufunction *func) static void rt_usb_hid_init(struct ufunction *func)
{ {
struct hid_s *hiddev; struct hid_s *hiddev;
hiddev = (struct hid_s *)func->user_data; hiddev = (struct hid_s *)func->user_data;
rt_memset(&hiddev->parent, 0, sizeof(hiddev->parent)); rt_memset(&hiddev->parent, 0, sizeof(hiddev->parent));
#ifdef RT_USING_DEVICE_OPS
hiddev->parent.ops = &hid_device_ops;
#else
hiddev->parent.write = _hid_write; hiddev->parent.write = _hid_write;
hiddev->func = func; #endif
hiddev->func = func;
rt_device_register(&hiddev->parent, "hidd", RT_DEVICE_FLAG_RDWR); rt_device_register(&hiddev->parent, "hidd", RT_DEVICE_FLAG_RDWR);
rt_mq_init(&hiddev->hid_mq, "hiddmq", hid_mq_pool, sizeof(struct hid_report), rt_mq_init(&hiddev->hid_mq, "hiddmq", hid_mq_pool, sizeof(struct hid_report),
sizeof(hid_mq_pool), RT_IPC_FLAG_FIFO); sizeof(hid_mq_pool), RT_IPC_FLAG_FIFO);

View File

@ -1180,6 +1180,19 @@ rt_err_t rt_rndis_eth_tx(rt_device_t dev, struct pbuf* p)
return result; return result;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops rndis_device_ops =
{
rt_rndis_eth_init,
rt_rndis_eth_open,
rt_rndis_eth_close,
rt_rndis_eth_read,
rt_rndis_eth_write,
rt_rndis_eth_control
};
#endif
#endif /* RT_USING_LWIP */ #endif /* RT_USING_LWIP */
#ifdef RNDIS_DELAY_LINK_UP #ifdef RNDIS_DELAY_LINK_UP
@ -1307,20 +1320,24 @@ ufunction_t rt_usbd_function_rndis_create(udevice_t device)
_rndis->host_addr[4] = 0xEA;//*(const rt_uint8_t *)(0x0FE081F1); _rndis->host_addr[4] = 0xEA;//*(const rt_uint8_t *)(0x0FE081F1);
_rndis->host_addr[5] = 0x13;//*(const rt_uint8_t *)(0x0FE081F2); _rndis->host_addr[5] = 0x13;//*(const rt_uint8_t *)(0x0FE081F2);
#ifdef RT_USING_DEVICE_OPS
_rndis->parent.parent.ops = &rndis_device_ops;
#else
_rndis->parent.parent.init = rt_rndis_eth_init; _rndis->parent.parent.init = rt_rndis_eth_init;
_rndis->parent.parent.open = rt_rndis_eth_open; _rndis->parent.parent.open = rt_rndis_eth_open;
_rndis->parent.parent.close = rt_rndis_eth_close; _rndis->parent.parent.close = rt_rndis_eth_close;
_rndis->parent.parent.read = rt_rndis_eth_read; _rndis->parent.parent.read = rt_rndis_eth_read;
_rndis->parent.parent.write = rt_rndis_eth_write; _rndis->parent.parent.write = rt_rndis_eth_write;
_rndis->parent.parent.control = rt_rndis_eth_control; _rndis->parent.parent.control = rt_rndis_eth_control;
#endif
_rndis->parent.parent.user_data = device; _rndis->parent.parent.user_data = device;
_rndis->parent.eth_rx = rt_rndis_eth_rx; _rndis->parent.eth_rx = rt_rndis_eth_rx;
_rndis->parent.eth_tx = rt_rndis_eth_tx; _rndis->parent.eth_tx = rt_rndis_eth_tx;
/* register eth device */ /* register eth device */
eth_device_init(&((rt_rndis_eth_t)cdc->user_data)->parent, "u0"); eth_device_init(&((rt_rndis_eth_t)cdc->user_data)->parent, "u0");
#endif /* RT_USING_LWIP */ #endif /* RT_USING_LWIP */
return cdc; return cdc;

View File

@ -246,16 +246,34 @@ static rt_err_t win_usb_control(rt_device_t dev, int cmd, void *args)
} }
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops winusb_device_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
win_usb_read,
win_usb_write,
win_usb_control,
};
#endif
static rt_err_t rt_usb_winusb_init(ufunction_t func) static rt_err_t rt_usb_winusb_init(ufunction_t func)
{ {
winusb_device_t winusb_device = (winusb_device_t)func->user_data; winusb_device_t winusb_device = (winusb_device_t)func->user_data;
winusb_device->parent.type = RT_Device_Class_Miscellaneous; winusb_device->parent.type = RT_Device_Class_Miscellaneous;
#ifdef RT_USING_DEVICE_OPS
winusb_device->parent.ops = &winusb_device_ops;
#else
winusb_device->parent.init = RT_NULL; winusb_device->parent.init = RT_NULL;
winusb_device->parent.open = RT_NULL; winusb_device->parent.open = RT_NULL;
winusb_device->parent.close = RT_NULL; winusb_device->parent.close = RT_NULL;
winusb_device->parent.read = win_usb_read; winusb_device->parent.read = win_usb_read;
winusb_device->parent.write = win_usb_write; winusb_device->parent.write = win_usb_write;
winusb_device->parent.control = win_usb_control; winusb_device->parent.control = win_usb_control;
#endif
winusb_device->parent.user_data = func; winusb_device->parent.user_data = func;

View File

@ -37,7 +37,7 @@ static const char* _adk_uri = RT_NULL;
static const char* _adk_serial = RT_NULL; static const char* _adk_serial = RT_NULL;
rt_err_t rt_usbh_adk_set_string(const char* manufacturer, const char* model, rt_err_t rt_usbh_adk_set_string(const char* manufacturer, const char* model,
const char* description, const char* _version, const char* uri, const char* description, const char* _version, const char* uri,
const char* serial) const char* serial)
{ {
_adk_manufacturer = manufacturer; _adk_manufacturer = manufacturer;
@ -62,31 +62,31 @@ RTM_EXPORT(rt_usbh_adk_set_string);
* @param intf the interface instance. * @param intf the interface instance.
* @duration the idle period of requesting data. * @duration the idle period of requesting data.
* @report_id the report id * @report_id the report id
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protocol) static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protocol)
{ {
struct urequest setup; struct urequest setup;
uinst_t device; uinst_t device;
int timeout = 100; int timeout = 100;
/* parameter check */ /* parameter check */
RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->device != RT_NULL); RT_ASSERT(intf->device != RT_NULL);
device = intf->device; device = intf->device;
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR | setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR |
USB_REQ_TYPE_DEVICE; USB_REQ_TYPE_DEVICE;
setup.request = USB_REQ_GET_PROTOCOL; setup.request = USB_REQ_GET_PROTOCOL;
setup.index = 0; setup.index = 0;
setup.length = 2; setup.length = 2;
setup.value = 0; setup.value = 0;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)protocol, 2, if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)protocol, 2,
timeout) == 0) return RT_EOK; timeout) == 0) return RT_EOK;
else return -RT_FALSE; else return -RT_FALSE;
} }
/** /**
@ -95,32 +95,32 @@ static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protoc
* @param intf the interface instance. * @param intf the interface instance.
* @duration the idle period of requesting data. * @duration the idle period of requesting data.
* @report_id the report id * @report_id the report id
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index, static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index,
const char* str) const char* str)
{ {
struct urequest setup; struct urequest setup;
uinst_t device; uinst_t device;
int timeout = 100; int timeout = 100;
/* parameter check */ /* parameter check */
RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->device != RT_NULL); RT_ASSERT(intf->device != RT_NULL);
device = intf->device; device = intf->device;
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
USB_REQ_TYPE_DEVICE; USB_REQ_TYPE_DEVICE;
setup.request = USB_REQ_SEND_STRING; setup.request = USB_REQ_SEND_STRING;
setup.index = index; setup.index = index;
setup.length = rt_strlen(str) + 1; setup.length = rt_strlen(str) + 1;
setup.value = 0; setup.value = 0;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)str, if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)str,
rt_strlen(str) + 1, timeout) == 0) return RT_EOK; rt_strlen(str) + 1, timeout) == 0) return RT_EOK;
else return -RT_FALSE; else return -RT_FALSE;
} }
/** /**
@ -129,41 +129,41 @@ static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index,
* @param intf the interface instance. * @param intf the interface instance.
* @duration the idle period of requesting data. * @duration the idle period of requesting data.
* @report_id the report id * @report_id the report id
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t rt_usbh_adk_start(struct uintf* intf) static rt_err_t rt_usbh_adk_start(struct uintf* intf)
{ {
struct urequest setup; struct urequest setup;
uinst_t device; uinst_t device;
int timeout = 100; int timeout = 100;
/* parameter check */ /* parameter check */
RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->device != RT_NULL); RT_ASSERT(intf->device != RT_NULL);
device = intf->device; device = intf->device;
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR | setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
USB_REQ_TYPE_DEVICE; USB_REQ_TYPE_DEVICE;
setup.request = USB_REQ_START; setup.request = USB_REQ_START;
setup.index = 0; setup.index = 0;
setup.length = 0; setup.length = 0;
setup.value = 0; setup.value = 0;
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0,
timeout) == 0) return RT_EOK; timeout) == 0) return RT_EOK;
else return -RT_FALSE; else return -RT_FALSE;
} }
/** /**
* This function will read data from usb adk device * This function will read data from usb adk device
* *
* @param intf the interface instance. * @param intf the interface instance.
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer, static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer,
rt_size_t size) rt_size_t size)
{ {
uadk_t adk; uadk_t adk;
@ -177,9 +177,9 @@ static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer
intf = (struct uintf*)device->user_data; intf = (struct uintf*)device->user_data;
adk = (uadk_t)intf->user_data; adk = (uadk_t)intf->user_data;
length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in, length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in,
buffer, size, 300); buffer, size, 300);
return length; return length;
} }
@ -188,33 +188,45 @@ static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer
* This function will write data to usb adk device * This function will write data to usb adk device
* *
* @param intf the interface instance. * @param intf the interface instance.
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_size_t rt_usbh_adk_write (rt_device_t device, rt_off_t pos, const void* buffer, static rt_size_t rt_usbh_adk_write (rt_device_t device, rt_off_t pos, const void* buffer,
rt_size_t size) rt_size_t size)
{ {
uadk_t adk; uadk_t adk;
rt_size_t length; rt_size_t length;
struct uintf* intf; struct uintf* intf;
RT_ASSERT(buffer != RT_NULL); RT_ASSERT(buffer != RT_NULL);
intf = (struct uintf*)device->user_data; intf = (struct uintf*)device->user_data;
adk = (uadk_t)intf->user_data; adk = (uadk_t)intf->user_data;
length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out, length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out,
(void*)buffer, size, 300); (void*)buffer, size, 300);
return length; return length;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops adk_device_ops =
{
RT_NULL;
RT_NULL;
RT_NULL;
rt_usbh_adk_read;
rt_usbh_adk_write;
RT_NULL;
};
#endif
/** /**
* This function will run adk class driver when usb device is detected and identified * This function will run adk class driver when usb device is detected and identified
* as a adk class device, it will continue the enumulate process. * as a adk class device, it will continue the enumulate process.
* *
* @param arg the argument. * @param arg the argument.
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t rt_usbh_adk_enable(void* arg) static rt_err_t rt_usbh_adk_enable(void* arg)
@ -224,8 +236,8 @@ static rt_err_t rt_usbh_adk_enable(void* arg)
struct uintf* intf = (struct uintf*)arg; struct uintf* intf = (struct uintf*)arg;
udev_desc_t dev_desc; udev_desc_t dev_desc;
rt_uint16_t protocol; rt_uint16_t protocol;
rt_err_t ret; rt_err_t ret;
/* parameter check */ /* parameter check */
if(intf == RT_NULL) if(intf == RT_NULL)
{ {
@ -234,72 +246,72 @@ static rt_err_t rt_usbh_adk_enable(void* arg)
} }
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_run\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_run\n"));
dev_desc = &intf->device->dev_desc; dev_desc = &intf->device->dev_desc;
if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID && if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID &&
(dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID || (dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID ||
dev_desc->idProduct == USB_ACCESSORY_ADB_PRODUCT_ID)) dev_desc->idProduct == USB_ACCESSORY_ADB_PRODUCT_ID))
{ {
if(intf->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR; if(intf->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR;
RT_DEBUG_LOG(RT_DEBUG_USB, ("found android accessory device\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("found android accessory device\n"));
} }
else else
{ {
RT_DEBUG_LOG(RT_DEBUG_USB, ("switch device\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("switch device\n"));
if((ret = rt_usbh_adk_get_protocol(intf, &protocol)) != RT_EOK) if((ret = rt_usbh_adk_get_protocol(intf, &protocol)) != RT_EOK)
{ {
rt_kprintf("rt_usbh_adk_get_protocol failed\n"); rt_kprintf("rt_usbh_adk_get_protocol failed\n");
return ret; return ret;
} }
if(protocol != 1) if(protocol != 1)
{ {
rt_kprintf("read protocol failed\n"); rt_kprintf("read protocol failed\n");
return -RT_ERROR; return -RT_ERROR;
} }
rt_usbh_adk_send_string(intf, rt_usbh_adk_send_string(intf,
ACCESSORY_STRING_MANUFACTURER, _adk_manufacturer); ACCESSORY_STRING_MANUFACTURER, _adk_manufacturer);
rt_usbh_adk_send_string(intf, rt_usbh_adk_send_string(intf,
ACCESSORY_STRING_MODEL, _adk_model); ACCESSORY_STRING_MODEL, _adk_model);
rt_usbh_adk_send_string(intf, rt_usbh_adk_send_string(intf,
ACCESSORY_STRING_DESCRIPTION, _adk_description); ACCESSORY_STRING_DESCRIPTION, _adk_description);
rt_usbh_adk_send_string(intf, rt_usbh_adk_send_string(intf,
ACCESSORY_STRING_VERSION, _adk_version); ACCESSORY_STRING_VERSION, _adk_version);
rt_usbh_adk_send_string(intf, rt_usbh_adk_send_string(intf,
ACCESSORY_STRING_URI, _adk_uri); ACCESSORY_STRING_URI, _adk_uri);
rt_usbh_adk_send_string(intf, rt_usbh_adk_send_string(intf,
ACCESSORY_STRING_SERIAL, _adk_serial); ACCESSORY_STRING_SERIAL, _adk_serial);
RT_DEBUG_LOG(RT_DEBUG_USB, ("manufacturer %s\n", _adk_manufacturer)); RT_DEBUG_LOG(RT_DEBUG_USB, ("manufacturer %s\n", _adk_manufacturer));
RT_DEBUG_LOG(RT_DEBUG_USB, ("model %s\n", _adk_model)); RT_DEBUG_LOG(RT_DEBUG_USB, ("model %s\n", _adk_model));
RT_DEBUG_LOG(RT_DEBUG_USB, ("description %s\n", _adk_description)); RT_DEBUG_LOG(RT_DEBUG_USB, ("description %s\n", _adk_description));
RT_DEBUG_LOG(RT_DEBUG_USB, ("version %s\n", _adk_version)); RT_DEBUG_LOG(RT_DEBUG_USB, ("version %s\n", _adk_version));
RT_DEBUG_LOG(RT_DEBUG_USB, ("uri %s\n", _adk_uri)); RT_DEBUG_LOG(RT_DEBUG_USB, ("uri %s\n", _adk_uri));
RT_DEBUG_LOG(RT_DEBUG_USB, ("serial %s\n", _adk_serial)); RT_DEBUG_LOG(RT_DEBUG_USB, ("serial %s\n", _adk_serial));
if((ret = rt_usbh_adk_start(intf)) != RT_EOK) if((ret = rt_usbh_adk_start(intf)) != RT_EOK)
{ {
rt_kprintf("rt_usbh_adk_start failed\n"); rt_kprintf("rt_usbh_adk_start failed\n");
return ret; return ret;
} }
return RT_EOK; return RT_EOK;
} }
adk = rt_malloc(sizeof(struct uadkinst)); adk = rt_malloc(sizeof(struct uadkinst));
RT_ASSERT(adk != RT_NULL); RT_ASSERT(adk != RT_NULL);
/* initilize the data structure */ /* initilize the data structure */
rt_memset(adk, 0, sizeof(struct uadkinst)); rt_memset(adk, 0, sizeof(struct uadkinst));
intf->user_data = (void*)adk; intf->user_data = (void*)adk;
for(i=0; i<intf->intf_desc->bNumEndpoints; i++) for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
{ {
uep_desc_t ep_desc; uep_desc_t ep_desc;
/* get endpoint descriptor from interface descriptor */ /* get endpoint descriptor from interface descriptor */
rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc); rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
if(ep_desc == RT_NULL) if(ep_desc == RT_NULL)
@ -307,24 +319,24 @@ static rt_err_t rt_usbh_adk_enable(void* arg)
rt_kprintf("rt_usb_get_endpoint_descriptor error\n"); rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
return -RT_ERROR; return -RT_ERROR;
} }
/* the endpoint type of adk class should be BULK */ /* the endpoint type of adk class should be BULK */
if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK) if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
continue; continue;
/* allocate pipes according to the endpoint type */ /* allocate pipes according to the endpoint type */
if(ep_desc->bEndpointAddress & USB_DIR_IN) if(ep_desc->bEndpointAddress & USB_DIR_IN)
{ {
/* allocate an in pipe for the adk instance */ /* allocate an in pipe for the adk instance */
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_in, ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_in,
intf, ep_desc, RT_NULL); intf, ep_desc, RT_NULL);
if(ret != RT_EOK) return ret; if(ret != RT_EOK) return ret;
} }
else else
{ {
/* allocate an output pipe for the adk instance */ /* allocate an output pipe for the adk instance */
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_out, ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_out,
intf, ep_desc, RT_NULL); intf, ep_desc, RT_NULL);
if(ret != RT_EOK) return ret; if(ret != RT_EOK) return ret;
} }
} }
@ -334,33 +346,37 @@ static rt_err_t rt_usbh_adk_enable(void* arg)
{ {
rt_kprintf("pipe error, unsupported device\n"); rt_kprintf("pipe error, unsupported device\n");
return -RT_ERROR; return -RT_ERROR;
} }
/* set configuration */ /* set configuration */
ret = rt_usbh_set_configure(intf->device, 1); ret = rt_usbh_set_configure(intf->device, 1);
if(ret != RT_EOK) return ret; if(ret != RT_EOK) return ret;
/* register adk device */ /* register adk device */
adk->device.type = RT_Device_Class_Char; adk->device.type = RT_Device_Class_Char;
adk->device.init = RT_NULL; #ifdef RT_USING_DEVICE_OPS
adk->device.open = RT_NULL; adk->device.ops = &adk_device_ops;
adk->device.close = RT_NULL; #else
adk->device.read = rt_usbh_adk_read; adk->device.init = RT_NULL;
adk->device.write = rt_usbh_adk_write; adk->device.open = RT_NULL;
adk->device.close = RT_NULL;
adk->device.read = rt_usbh_adk_read;
adk->device.write = rt_usbh_adk_write;
adk->device.control = RT_NULL; adk->device.control = RT_NULL;
#endif
adk->device.user_data = (void*)intf; adk->device.user_data = (void*)intf;
rt_device_register(&adk->device, "adkdev", RT_DEVICE_FLAG_RDWR); rt_device_register(&adk->device, "adkdev", RT_DEVICE_FLAG_RDWR);
return RT_EOK; return RT_EOK;
} }
/** /**
* This function will be invoked when usb device plug out is detected and it would clean * This function will be invoked when usb device plug out is detected and it would clean
* and release all hub class related resources. * and release all hub class related resources.
* *
* @param arg the argument. * @param arg the argument.
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t rt_usbh_adk_disable(void* arg) static rt_err_t rt_usbh_adk_disable(void* arg)
@ -373,12 +389,12 @@ static rt_err_t rt_usbh_adk_disable(void* arg)
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_stop\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_stop\n"));
adk = (uadk_t)intf->user_data; adk = (uadk_t)intf->user_data;
if(adk == RT_NULL) if(adk == RT_NULL)
{ {
rt_free(intf); rt_free(intf);
return RT_EOK; return RT_EOK;
} }
if(adk->pipe_in != RT_NULL) if(adk->pipe_in != RT_NULL)
rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_in); rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_in);
@ -389,11 +405,11 @@ static rt_err_t rt_usbh_adk_disable(void* arg)
rt_device_unregister(&adk->device); rt_device_unregister(&adk->device);
/* free adk instance */ /* free adk instance */
if(adk != RT_NULL) if(adk != RT_NULL)
{ {
rt_free(adk); rt_free(adk);
} }
/* free interface instance */ /* free interface instance */
rt_free(intf); rt_free(intf);
@ -403,13 +419,13 @@ static rt_err_t rt_usbh_adk_disable(void* arg)
/** /**
* This function will register adk class driver to the usb class driver manager. * This function will register adk class driver to the usb class driver manager.
* and it should be invoked in the usb system initialization. * and it should be invoked in the usb system initialization.
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
ucd_t rt_usbh_class_driver_adk(void) ucd_t rt_usbh_class_driver_adk(void)
{ {
adk_driver.class_code = USB_CLASS_ADK; adk_driver.class_code = USB_CLASS_ADK;
adk_driver.enable = rt_usbh_adk_enable; adk_driver.enable = rt_usbh_adk_enable;
adk_driver.disable = rt_usbh_adk_disable; adk_driver.disable = rt_usbh_adk_disable;

View File

@ -31,7 +31,7 @@ static int udisk_get_id(void)
if((_udisk_idset & (1 << i)) != 0) continue; if((_udisk_idset & (1 << i)) != 0) continue;
else break; else break;
} }
/* it should not happen */ /* it should not happen */
if(i == UDISK_MAX_COUNT) RT_ASSERT(0); if(i == UDISK_MAX_COUNT) RT_ASSERT(0);
@ -43,14 +43,14 @@ static void udisk_free_id(int id)
{ {
RT_ASSERT(id < UDISK_MAX_COUNT) RT_ASSERT(id < UDISK_MAX_COUNT)
_udisk_idset &= ~(1 << id); _udisk_idset &= ~(1 << id);
} }
/** /**
* This function will initialize the udisk device * This function will initialize the udisk device
* *
* @param dev the pointer of device driver structure * @param dev the pointer of device driver structure
* *
* @return RT_EOK * @return RT_EOK
*/ */
static rt_err_t rt_udisk_init(rt_device_t dev) static rt_err_t rt_udisk_init(rt_device_t dev)
@ -68,7 +68,7 @@ static rt_err_t rt_udisk_init(rt_device_t dev)
* *
* @return the actually read size on successful, otherwise negative returned. * @return the actually read size on successful, otherwise negative returned.
*/ */
static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer, static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer,
rt_size_t size) rt_size_t size)
{ {
rt_err_t ret; rt_err_t ret;
@ -81,18 +81,18 @@ static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer,
RT_ASSERT(buffer != RT_NULL); RT_ASSERT(buffer != RT_NULL);
if(size > 4096) timeout = 800; if(size > 4096) timeout = 800;
data = (struct ustor_data*)dev->user_data; data = (struct ustor_data*)dev->user_data;
intf = data->intf; intf = data->intf;
ret = rt_usbh_storage_read10(intf, (rt_uint8_t*)buffer, pos, size, timeout); ret = rt_usbh_storage_read10(intf, (rt_uint8_t*)buffer, pos, size, timeout);
if (ret != RT_EOK) if (ret != RT_EOK)
{ {
rt_kprintf("usb mass_storage read failed\n"); rt_kprintf("usb mass_storage read failed\n");
return 0; return 0;
} }
return size; return size;
} }
@ -106,7 +106,7 @@ static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer,
* *
* @return the actually written size on successful, otherwise negative returned. * @return the actually written size on successful, otherwise negative returned.
*/ */
static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buffer, static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buffer,
rt_size_t size) rt_size_t size)
{ {
rt_err_t ret; rt_err_t ret;
@ -129,7 +129,7 @@ static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buff
rt_kprintf("usb mass_storage write %d sector failed\n", size); rt_kprintf("usb mass_storage write %d sector failed\n", size);
return 0; return 0;
} }
return size; return size;
} }
@ -138,9 +138,9 @@ static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buff
* *
* @param intf the interface instance. * @param intf the interface instance.
* @param buffer the data buffer to save inquiry data * @param buffer the data buffer to save inquiry data
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args) static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args)
{ {
ustor_t stor; ustor_t stor;
@ -164,14 +164,26 @@ static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args)
geometry->sector_count = stor->capicity[0]; geometry->sector_count = stor->capicity[0];
} }
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops udisk_device_ops =
{
rt_udisk_init,
RT_NULL,
RT_NULL,
rt_udisk_read,
rt_udisk_write,
rt_udisk_control
};
#endif
/** /**
* This function will run udisk driver when usb disk is detected. * This function will run udisk driver when usb disk is detected.
* *
* @param intf the usb interface instance. * @param intf the usb interface instance.
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_udisk_run(struct uhintf* intf) rt_err_t rt_udisk_run(struct uhintf* intf)
@ -179,10 +191,10 @@ rt_err_t rt_udisk_run(struct uhintf* intf)
int i = 0; int i = 0;
rt_err_t ret; rt_err_t ret;
char dname[4]; char dname[4];
char sname[8]; char sname[8];
rt_uint8_t max_lun, *sector, sense[18], inquiry[36]; rt_uint8_t max_lun, *sector, sense[18], inquiry[36];
struct dfs_partition part[MAX_PARTITION_COUNT]; struct dfs_partition part[MAX_PARTITION_COUNT];
ustor_t stor; ustor_t stor;
/* check parameter */ /* check parameter */
RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf != RT_NULL);
@ -193,40 +205,40 @@ rt_err_t rt_udisk_run(struct uhintf* intf)
// rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT); // rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
/* reset mass storage class device */ /* reset mass storage class device */
ret = rt_usbh_storage_reset(intf); ret = rt_usbh_storage_reset(intf);
if(ret != RT_EOK) return ret; if(ret != RT_EOK) return ret;
stor = (ustor_t)intf->user_data; stor = (ustor_t)intf->user_data;
/* get max logic unit number */ /* get max logic unit number */
ret = rt_usbh_storage_get_max_lun(intf, &max_lun); ret = rt_usbh_storage_get_max_lun(intf, &max_lun);
if(ret != RT_EOK) if(ret != RT_EOK)
rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT); rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
/* reset pipe in endpoint */ /* reset pipe in endpoint */
if(stor->pipe_in->status == UPIPE_STATUS_STALL) if(stor->pipe_in->status == UPIPE_STATUS_STALL)
{ {
ret = rt_usbh_clear_feature(intf->device, ret = rt_usbh_clear_feature(intf->device,
stor->pipe_in->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT); stor->pipe_in->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
if(ret != RT_EOK) return ret; if(ret != RT_EOK) return ret;
} }
/* reset pipe out endpoint */ /* reset pipe out endpoint */
if(stor->pipe_out->status == UPIPE_STATUS_STALL) if(stor->pipe_out->status == UPIPE_STATUS_STALL)
{ {
ret = rt_usbh_clear_feature(intf->device, ret = rt_usbh_clear_feature(intf->device,
stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT); stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
if(ret != RT_EOK) return ret; if(ret != RT_EOK) return ret;
} }
while((ret = rt_usbh_storage_inquiry(intf, inquiry)) != RT_EOK) while((ret = rt_usbh_storage_inquiry(intf, inquiry)) != RT_EOK)
{ {
if(ret == -RT_EIO) return ret; if(ret == -RT_EIO) return ret;
rt_thread_delay(5); rt_thread_delay(5);
if(i++ < 10) continue; if(i++ < 10) continue;
rt_kprintf("rt_usbh_storage_inquiry error\n"); rt_kprintf("rt_usbh_storage_inquiry error\n");
return -RT_ERROR; return -RT_ERROR;
} }
i = 0; i = 0;
@ -235,26 +247,26 @@ rt_err_t rt_udisk_run(struct uhintf* intf)
while((ret = rt_usbh_storage_test_unit_ready(intf)) != RT_EOK) while((ret = rt_usbh_storage_test_unit_ready(intf)) != RT_EOK)
{ {
if(ret == -RT_EIO) return ret; if(ret == -RT_EIO) return ret;
ret = rt_usbh_storage_request_sense(intf, sense); ret = rt_usbh_storage_request_sense(intf, sense);
if(ret == -RT_EIO) return ret; if(ret == -RT_EIO) return ret;
rt_thread_delay(10); rt_thread_delay(10);
if(i++ < 10) continue; if(i++ < 10) continue;
rt_kprintf("rt_usbh_storage_test_unit_ready error\n"); rt_kprintf("rt_usbh_storage_test_unit_ready error\n");
return -RT_ERROR; return -RT_ERROR;
} }
i = 0; i = 0;
rt_memset(stor->capicity, 0, sizeof(stor->capicity)); rt_memset(stor->capicity, 0, sizeof(stor->capicity));
/* get storage capacity */ /* get storage capacity */
while((ret = rt_usbh_storage_get_capacity(intf, while((ret = rt_usbh_storage_get_capacity(intf,
(rt_uint8_t*)stor->capicity)) != RT_EOK) (rt_uint8_t*)stor->capicity)) != RT_EOK)
{ {
if(ret == -RT_EIO) return ret; if(ret == -RT_EIO) return ret;
rt_thread_delay(50); rt_thread_delay(50);
if(i++ < 10) continue; if(i++ < 10) continue;
@ -262,16 +274,16 @@ rt_err_t rt_udisk_run(struct uhintf* intf)
stor->capicity[1] = 0x200; stor->capicity[1] = 0x200;
rt_kprintf("rt_usbh_storage_get_capacity error\n"); rt_kprintf("rt_usbh_storage_get_capacity error\n");
break; break;
} }
stor->capicity[0] = uswap_32(stor->capicity[0]); stor->capicity[0] = uswap_32(stor->capicity[0]);
stor->capicity[1] = uswap_32(stor->capicity[1]); stor->capicity[1] = uswap_32(stor->capicity[1]);
stor->capicity[0] += 1; stor->capicity[0] += 1;
RT_DEBUG_LOG(RT_DEBUG_USB, ("capicity %d, block size %d\n", RT_DEBUG_LOG(RT_DEBUG_USB, ("capicity %d, block size %d\n",
stor->capicity[0], stor->capicity[1])); stor->capicity[0], stor->capicity[1]));
/* get the first sector to read partition table */ /* get the first sector to read partition table */
sector = (rt_uint8_t*) rt_malloc (SECTOR_SIZE); sector = (rt_uint8_t*) rt_malloc (SECTOR_SIZE);
if (sector == RT_NULL) if (sector == RT_NULL)
@ -283,42 +295,46 @@ rt_err_t rt_udisk_run(struct uhintf* intf)
rt_memset(sector, 0, SECTOR_SIZE); rt_memset(sector, 0, SECTOR_SIZE);
RT_DEBUG_LOG(RT_DEBUG_USB, ("read partition table\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("read partition table\n"));
/* get the partition table */ /* get the partition table */
ret = rt_usbh_storage_read10(intf, sector, 0, 1, 500); ret = rt_usbh_storage_read10(intf, sector, 0, 1, 500);
if(ret != RT_EOK) if(ret != RT_EOK)
{ {
rt_kprintf("read parition table error\n"); rt_kprintf("read parition table error\n");
rt_free(sector); rt_free(sector);
return -RT_ERROR; return -RT_ERROR;
} }
RT_DEBUG_LOG(RT_DEBUG_USB, ("finished reading partition\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("finished reading partition\n"));
for(i=0; i<MAX_PARTITION_COUNT; i++) for(i=0; i<MAX_PARTITION_COUNT; i++)
{ {
/* get the first partition */ /* get the first partition */
ret = dfs_filesystem_get_partition(&part[i], sector, i); ret = dfs_filesystem_get_partition(&part[i], sector, i);
if (ret == RT_EOK) if (ret == RT_EOK)
{ {
struct ustor_data* data = rt_malloc(sizeof(struct ustor_data)); struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
rt_memset(data, 0, sizeof(struct ustor_data)); rt_memset(data, 0, sizeof(struct ustor_data));
data->intf = intf; data->intf = intf;
data->udisk_id = udisk_get_id(); data->udisk_id = udisk_get_id();
rt_snprintf(dname, 6, "ud%d-%d", data->udisk_id, i); rt_snprintf(dname, 6, "ud%d-%d", data->udisk_id, i);
rt_snprintf(sname, 8, "sem_ud%d", i); rt_snprintf(sname, 8, "sem_ud%d", i);
data->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO); data->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
/* register sdcard device */ /* register sdcard device */
stor->dev[i].type = RT_Device_Class_Block; stor->dev[i].type = RT_Device_Class_Block;
stor->dev[i].init = rt_udisk_init; #ifdef RT_USING_DEVICE_OPS
stor->dev[i].read = rt_udisk_read; stor->dev[i].ops = &udisk_device_ops;
stor->dev[i].write = rt_udisk_write; #else
stor->dev[i].init = rt_udisk_init;
stor->dev[i].read = rt_udisk_read;
stor->dev[i].write = rt_udisk_write;
stor->dev[i].control = rt_udisk_control; stor->dev[i].control = rt_udisk_control;
#endif
stor->dev[i].user_data = (void*)data; stor->dev[i].user_data = (void*)data;
rt_device_register(&stor->dev[i], dname, RT_DEVICE_FLAG_RDWR | rt_device_register(&stor->dev[i], dname, RT_DEVICE_FLAG_RDWR |
RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
stor->dev_cnt++; stor->dev_cnt++;
@ -330,16 +346,16 @@ rt_err_t rt_udisk_run(struct uhintf* intf)
else else
{ {
RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount failed\n", i)); RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount failed\n", i));
} }
} }
else else
{ {
if(i == 0) if(i == 0)
{ {
struct ustor_data* data = rt_malloc(sizeof(struct ustor_data)); struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
rt_memset(data, 0, sizeof(struct ustor_data)); rt_memset(data, 0, sizeof(struct ustor_data));
data->udisk_id = udisk_get_id(); data->udisk_id = udisk_get_id();
/* there is no partition table */ /* there is no partition table */
data->part.offset = 0; data->part.offset = 0;
data->part.size = 0; data->part.size = 0;
@ -349,23 +365,27 @@ rt_err_t rt_udisk_run(struct uhintf* intf)
rt_snprintf(dname, 7, "udisk%d", data->udisk_id); rt_snprintf(dname, 7, "udisk%d", data->udisk_id);
/* register sdcard device */ /* register sdcard device */
stor->dev[0].type = RT_Device_Class_Block; stor->dev[0].type = RT_Device_Class_Block;
stor->dev[0].init = rt_udisk_init; #ifdef RT_USING_DEVICE_OPS
stor->dev[0].read = rt_udisk_read; stor->dev[i].ops = &udisk_device_ops;
stor->dev[0].write = rt_udisk_write; #else
stor->dev[0].init = rt_udisk_init;
stor->dev[0].read = rt_udisk_read;
stor->dev[0].write = rt_udisk_write;
stor->dev[0].control = rt_udisk_control; stor->dev[0].control = rt_udisk_control;
#endif
stor->dev[0].user_data = (void*)data; stor->dev[0].user_data = (void*)data;
rt_device_register(&stor->dev[0], dname, rt_device_register(&stor->dev[0], dname,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE
| RT_DEVICE_FLAG_STANDALONE); | RT_DEVICE_FLAG_STANDALONE);
stor->dev_cnt++; stor->dev_cnt++;
if (dfs_mount(stor->dev[0].parent.name, UDISK_MOUNTPOINT, if (dfs_mount(stor->dev[0].parent.name, UDISK_MOUNTPOINT,
"elm", 0, 0) == 0) "elm", 0, 0) == 0)
{ {
rt_kprintf("Mount FAT on Udisk successful.\n"); rt_kprintf("Mount FAT on Udisk successful.\n");
} }
else else
{ {
rt_kprintf("Mount FAT on Udisk failed.\n"); rt_kprintf("Mount FAT on Udisk failed.\n");
@ -382,31 +402,31 @@ rt_err_t rt_udisk_run(struct uhintf* intf)
} }
/** /**
* This function will be invoked when usb disk plug out is detected and it would clean * This function will be invoked when usb disk plug out is detected and it would clean
* and release all udisk related resources. * and release all udisk related resources.
* *
* @param intf the usb interface instance. * @param intf the usb interface instance.
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_udisk_stop(struct uhintf* intf) rt_err_t rt_udisk_stop(struct uhintf* intf)
{ {
int i; int i;
ustor_t stor; ustor_t stor;
struct ustor_data* data; struct ustor_data* data;
/* check parameter */ /* check parameter */
RT_ASSERT(intf != RT_NULL); RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->device != RT_NULL); RT_ASSERT(intf->device != RT_NULL);
stor = (ustor_t)intf->user_data; stor = (ustor_t)intf->user_data;
RT_ASSERT(stor != RT_NULL); RT_ASSERT(stor != RT_NULL);
for(i=0; i<stor->dev_cnt; i++) for(i=0; i<stor->dev_cnt; i++)
{ {
rt_device_t dev = &stor->dev[i]; rt_device_t dev = &stor->dev[i];
data = (struct ustor_data*)dev->user_data; data = (struct ustor_data*)dev->user_data;
/* unmount filesystem */ /* unmount filesystem */
dfs_unmount(UDISK_MOUNTPOINT); dfs_unmount(UDISK_MOUNTPOINT);

View File

@ -78,6 +78,18 @@ static rt_err_t rt_watchdog_control(struct rt_device *dev,
return (wtd->ops->control(wtd, cmd, args)); return (wtd->ops->control(wtd, cmd, args));
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops wdt_ops =
{
rt_watchdog_init,
rt_watchdog_open,
rt_watchdog_close,
RT_NULL,
RT_NULL,
rt_watchdog_control,
};
#endif
/** /**
* This function register a watchdog device * This function register a watchdog device
*/ */
@ -95,12 +107,16 @@ rt_err_t rt_hw_watchdog_register(struct rt_watchdog_device *wtd,
device->rx_indicate = RT_NULL; device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL; device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &wdt_ops;
#else
device->init = rt_watchdog_init; device->init = rt_watchdog_init;
device->open = rt_watchdog_open; device->open = rt_watchdog_open;
device->close = rt_watchdog_close; device->close = rt_watchdog_close;
device->read = RT_NULL; device->read = RT_NULL;
device->write = RT_NULL; device->write = RT_NULL;
device->control = rt_watchdog_control; device->control = rt_watchdog_control;
#endif
device->user_data = data; device->user_data = data;
/* register a character device */ /* register a character device */

View File

@ -35,6 +35,8 @@
#include <pthread.h> #include <pthread.h>
#endif #endif
int _EXFUN(putenv,(char *__string));
int libc_system_init(void) int libc_system_init(void)
{ {
#if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS) #if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS)

View File

@ -31,6 +31,8 @@
#define STDIO_DEVICE_NAME_MAX 32 #define STDIO_DEVICE_NAME_MAX 32
int _EXFUN(fileno, (FILE *));
static FILE* std_console = NULL; static FILE* std_console = NULL;
int libc_stdio_set_console(const char* device_name, int mode) int libc_stdio_set_console(const char* device_name, int mode)

View File

@ -112,6 +112,18 @@ static rt_err_t fdevice_control(rt_device_t dev, int cmd, void *arg)
return RT_EOK; return RT_EOK;
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops log_trace_ops =
{
RT_NULL,
fdevice_open,
fdevice_close,
RT_NULL,
fdevice_write,
fdevice_control
};
#endif
void log_trace_file_init(const char *filename) void log_trace_file_init(const char *filename)
{ {
rt_device_t device; rt_device_t device;
@ -123,11 +135,15 @@ void log_trace_file_init(const char *filename)
_file_device.parent.type = RT_Device_Class_Char; _file_device.parent.type = RT_Device_Class_Char;
#ifdef RT_USING_DEVICE_OPS
_file_device.parent.ops = &log_trace_ops;
#else
_file_device.parent.init = RT_NULL; _file_device.parent.init = RT_NULL;
_file_device.parent.open = fdevice_open; _file_device.parent.open = fdevice_open;
_file_device.parent.close = fdevice_close; _file_device.parent.close = fdevice_close;
_file_device.parent.write = fdevice_write; _file_device.parent.write = fdevice_write;
_file_device.parent.control = fdevice_control; _file_device.parent.control = fdevice_control;
#endif
rt_device_register(&_file_device.parent, "logfile", O_RDWR); rt_device_register(&_file_device.parent, "logfile", O_RDWR);
} }

View File

@ -383,17 +383,33 @@ static rt_err_t _log_control(rt_device_t dev, int cmd, void *arg)
return rt_device_control(_traceout_device, cmd, arg); return rt_device_control(_traceout_device, cmd, arg);
} }
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops log_device_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
_log_write,
_log_control
};
#endif
int log_trace_init(void) int log_trace_init(void)
{ {
rt_memset(&_log_device, 0x00, sizeof(_log_device)); rt_memset(&_log_device, 0x00, sizeof(_log_device));
_log_device.type = RT_Device_Class_Char; _log_device.type = RT_Device_Class_Char;
#ifdef RT_USING_DEVICE_OPS
_log_device.ops = &log_device_ops;
#else
_log_device.init = RT_NULL; _log_device.init = RT_NULL;
_log_device.open = RT_NULL; _log_device.open = RT_NULL;
_log_device.close = RT_NULL; _log_device.close = RT_NULL;
_log_device.read = RT_NULL; _log_device.read = RT_NULL;
_log_device.write = _log_write; _log_device.write = _log_write;
_log_device.control = _log_control; _log_device.control = _log_control;
#endif
/* no indication and complete callback */ /* no indication and complete callback */
_log_device.rx_indicate = RT_NULL; _log_device.rx_indicate = RT_NULL;

View File

@ -848,6 +848,17 @@ enum rt_device_class_type
#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x13 /**< set alarm */ #define RT_DEVICE_CTRL_RTC_SET_ALARM 0x13 /**< set alarm */
typedef struct rt_device *rt_device_t; typedef struct rt_device *rt_device_t;
struct rt_device_ops
{
/* common device interface */
rt_err_t (*init) (rt_device_t dev);
rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
rt_err_t (*close) (rt_device_t dev);
rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
};
/** /**
* Device structure * Device structure
*/ */
@ -866,6 +877,9 @@ struct rt_device
rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size); rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
rt_err_t (*tx_complete)(rt_device_t dev, void *buffer); rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);
#ifdef RT_USING_DEVICE_OPS
const struct rt_device_ops *ops;
#else
/* common device interface */ /* common device interface */
rt_err_t (*init) (rt_device_t dev); rt_err_t (*init) (rt_device_t dev);
rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag); rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
@ -873,6 +887,7 @@ struct rt_device
rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size); rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
rt_err_t (*control)(rt_device_t dev, int cmd, void *args); rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
#endif
#if defined(RT_USING_POSIX) #if defined(RT_USING_POSIX)
const struct dfs_file_ops *fops; const struct dfs_file_ops *fops;
@ -1030,8 +1045,8 @@ struct rt_module
rt_uint32_t user_data; /**< arch data in the module */ rt_uint32_t user_data; /**< arch data in the module */
/* object in this module, module object is the last basic object type */ void (*module_init)(void);
struct rt_object_information module_object[RT_Object_Class_Unknown]; void (*module_cleanup)(void);
}; };
typedef struct rt_module *rt_module_t; typedef struct rt_module *rt_module_t;

View File

@ -192,6 +192,10 @@ menu "Kernel Device Object"
bool "Using device object" bool "Using device object"
default y default y
config RT_USING_DEVICE_OPS
bool "Using ops for each device object"
default n
config RT_USING_INTERRUPT_INFO config RT_USING_INTERRUPT_INFO
bool "Enable additional interrupt trace information" bool "Enable additional interrupt trace information"
default n default n

View File

@ -32,12 +32,28 @@
#ifdef RT_USING_DEVICE #ifdef RT_USING_DEVICE
#ifdef RT_USING_DEVICE_OPS
#define device_init (dev->ops->init)
#define device_open (dev->ops->open)
#define device_close (dev->ops->close)
#define device_read (dev->ops->read)
#define device_write (dev->ops->write)
#define device_control (dev->ops->control)
#else
#define device_init (dev->init)
#define device_open (dev->open)
#define device_close (dev->close)
#define device_read (dev->read)
#define device_write (dev->write)
#define device_control (dev->control)
#endif
/** /**
* This function registers a device driver with specified name. * This function registers a device driver with specified name.
* *
* @param dev the pointer of device driver structure * @param dev the pointer of device driver structure
* @param name the device driver's name * @param name the device driver's name
* @param flags the flag of device * @param flags the capabilities flag of device
* *
* @return the error code, RT_EOK on initialization successfully. * @return the error code, RT_EOK on initialization successfully.
*/ */
@ -154,6 +170,8 @@ rt_device_t rt_device_create(int type, int attach_size)
rt_device_t device; rt_device_t device;
size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE); size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE);
attach_size = RT_ALIGN(attach_size, RT_ALIGN_SIZE);
/* use the totoal size */
size += attach_size; size += attach_size;
device = (rt_device_t)rt_malloc(size); device = (rt_device_t)rt_malloc(size);
@ -197,11 +215,11 @@ rt_err_t rt_device_init(rt_device_t dev)
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
/* get device init handler */ /* get device init handler */
if (dev->init != RT_NULL) if (device_init != RT_NULL)
{ {
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
{ {
result = dev->init(dev); result = device_init(dev);
if (result != RT_EOK) if (result != RT_EOK)
{ {
rt_kprintf("To initialize device:%s failed. The error code is %d\n", rt_kprintf("To initialize device:%s failed. The error code is %d\n",
@ -234,9 +252,9 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
/* if device is not initialized, initialize it. */ /* if device is not initialized, initialize it. */
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
{ {
if (dev->init != RT_NULL) if (device_init != RT_NULL)
{ {
result = dev->init(dev); result = device_init(dev);
if (result != RT_EOK) if (result != RT_EOK)
{ {
rt_kprintf("To initialize device:%s failed. The error code is %d\n", rt_kprintf("To initialize device:%s failed. The error code is %d\n",
@ -257,9 +275,9 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
} }
/* call device open interface */ /* call device open interface */
if (dev->open != RT_NULL) if (device_open != RT_NULL)
{ {
result = dev->open(dev, oflag); result = device_open(dev, oflag);
} }
else else
{ {
@ -304,9 +322,9 @@ rt_err_t rt_device_close(rt_device_t dev)
return RT_EOK; return RT_EOK;
/* call device close interface */ /* call device close interface */
if (dev->close != RT_NULL) if (device_close != RT_NULL)
{ {
result = dev->close(dev); result = device_close(dev);
} }
/* set open flag */ /* set open flag */
@ -343,9 +361,9 @@ rt_size_t rt_device_read(rt_device_t dev,
} }
/* call device read interface */ /* call device read interface */
if (dev->read != RT_NULL) if (device_read != RT_NULL)
{ {
return dev->read(dev, pos, buffer, size); return device_read(dev, pos, buffer, size);
} }
/* set error code */ /* set error code */
@ -381,9 +399,9 @@ rt_size_t rt_device_write(rt_device_t dev,
} }
/* call device write interface */ /* call device write interface */
if (dev->write != RT_NULL) if (device_write != RT_NULL)
{ {
return dev->write(dev, pos, buffer, size); return device_write(dev, pos, buffer, size);
} }
/* set error code */ /* set error code */
@ -407,9 +425,9 @@ rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
/* call device write interface */ /* call device write interface */
if (dev->control != RT_NULL) if (device_control != RT_NULL)
{ {
return dev->control(dev, cmd, arg); return device_control(dev, cmd, arg);
} }
return -RT_ENOSYS; return -RT_ENOSYS;