mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-02-05 15:23:03 +08:00
[Kernel] Add device ops feature.
This commit is contained in:
@@ -421,6 +421,18 @@ static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args)
|
||||
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)
|
||||
{
|
||||
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->tx_complete = RT_NULL;
|
||||
|
||||
device->init = _audio_dev_init;
|
||||
device->open = _audio_dev_open;
|
||||
device->close = _audio_dev_close;
|
||||
device->read = _audio_dev_read;
|
||||
device->write = _audio_dev_write;
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
device->ops = &audio_ops;
|
||||
#else
|
||||
device->init = _audio_dev_init;
|
||||
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;
|
||||
#endif
|
||||
device->user_data = data;
|
||||
|
||||
//init memory pool for replay
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
struct rt_audio_pipe *pipe;
|
||||
|
||||
|
||||
pipe = (struct rt_audio_pipe *)dev;
|
||||
|
||||
|
||||
if (cmd == PIPE_CTRL_GET_SPACE && args)
|
||||
*(rt_size_t*)args = rt_ringbuffer_space_len(&pipe->ringbuffer);
|
||||
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
|
||||
* resource management.
|
||||
@@ -233,12 +245,16 @@ rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe,
|
||||
|
||||
/* create 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.open = RT_NULL;
|
||||
pipe->parent.close = RT_NULL;
|
||||
pipe->parent.read = rt_pipe_read;
|
||||
pipe->parent.write = rt_pipe_write;
|
||||
pipe->parent.control = rt_pipe_control;
|
||||
#endif
|
||||
|
||||
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user