2022-11-20 22:16:41 +08:00
|
|
|
#include "pika_hal.h"
|
|
|
|
#include "PikaObj.h"
|
|
|
|
|
2022-11-20 23:55:03 +08:00
|
|
|
#define PIKA_HAL_TABLE_FILE_API
|
|
|
|
#include "pika_hal_table.h"
|
2022-11-20 22:16:41 +08:00
|
|
|
|
2022-11-21 16:37:52 +08:00
|
|
|
static const pika_dev_impl pika_dev_impl_list[] = {
|
2022-11-20 23:55:03 +08:00
|
|
|
#define PIKA_HAL_TABLE_IMPL
|
|
|
|
#include "pika_hal_table.h"
|
2022-11-20 22:16:41 +08:00
|
|
|
};
|
|
|
|
|
2022-11-21 16:37:52 +08:00
|
|
|
static pika_dev_impl* _pika_dev_get_impl(PIKA_HAL_DEV_TYPE type) {
|
2022-11-20 23:55:03 +08:00
|
|
|
if (type >= _PIKA_DEV_TYPE_MAX) {
|
2022-11-20 22:16:41 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (pika_dev_impl*)&pika_dev_impl_list[type];
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:37:52 +08:00
|
|
|
pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name) {
|
2022-11-20 23:55:03 +08:00
|
|
|
if (dev_type >= _PIKA_DEV_TYPE_MAX) {
|
2022-11-21 17:47:43 +08:00
|
|
|
__platform_printf("Error: dev_type invalied.\r\n");
|
2022-11-20 22:16:41 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pika_dev_impl* impl = _pika_dev_get_impl(dev_type);
|
|
|
|
if (impl->open == NULL) {
|
2022-11-21 17:47:43 +08:00
|
|
|
__platform_printf("Error: dev_open_impl not implamented.\r\n");
|
2022-11-20 22:16:41 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pika_dev* dev = (pika_dev*)pikaMalloc(sizeof(pika_dev));
|
|
|
|
if (dev == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dev->type = dev_type;
|
2022-11-21 16:37:52 +08:00
|
|
|
if (impl->open(dev, name) != 0) {
|
2022-11-20 22:16:41 +08:00
|
|
|
pikaFree(dev, sizeof(pika_dev));
|
2022-11-21 17:47:43 +08:00
|
|
|
__platform_printf("Error: dev_open failed.\r\n");
|
2022-11-20 22:16:41 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pika_hal_close(pika_dev* dev) {
|
|
|
|
if (dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pika_dev_impl* impl = _pika_dev_get_impl(dev->type);
|
|
|
|
if (impl->close == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret = impl->close(dev);
|
|
|
|
pikaFree(dev, sizeof(pika_dev));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:37:52 +08:00
|
|
|
int pika_hal_read(pika_dev* dev, void* buf, size_t len) {
|
2022-11-20 22:16:41 +08:00
|
|
|
if (dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pika_dev_impl* impl = _pika_dev_get_impl(dev->type);
|
|
|
|
if (impl->read == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return impl->read(dev, buf, len);
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:37:52 +08:00
|
|
|
int pika_hal_write(pika_dev* dev, void* buf, size_t len) {
|
2022-11-20 22:16:41 +08:00
|
|
|
if (dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pika_dev_impl* impl = _pika_dev_get_impl(dev->type);
|
|
|
|
if (impl->write == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return impl->write(dev, buf, len);
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:37:52 +08:00
|
|
|
static const int _pika_hal_cmd_arg_cnt[_PIKA_HAL_IOCTL_MAX] = {
|
|
|
|
[PIKA_HAL_IOCTL_ENABLE] = 0,
|
|
|
|
[PIKA_HAL_IOCTL_CONFIG] = 1,
|
2022-11-21 17:47:43 +08:00
|
|
|
[PIKA_HAL_IOCTL_DISABLE] = 0,
|
2022-11-20 22:16:41 +08:00
|
|
|
};
|
|
|
|
|
2022-11-21 16:37:52 +08:00
|
|
|
static int _pika_hal_get_arg_cnt(PIKA_HAL_IOCTL_CMD cmd) {
|
|
|
|
if (cmd >= _PIKA_HAL_IOCTL_MAX) {
|
2022-11-20 22:16:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return _pika_hal_cmd_arg_cnt[cmd];
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:37:52 +08:00
|
|
|
int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
|
2022-11-20 22:16:41 +08:00
|
|
|
if (dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int arg_cnt = _pika_hal_get_arg_cnt(cmd);
|
|
|
|
if (arg_cnt < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pika_dev_impl* impl = _pika_dev_get_impl(dev->type);
|
|
|
|
if (impl->ioctl == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
va_list args;
|
|
|
|
va_start(args, arg_cnt);
|
|
|
|
uintptr_t arg = va_arg(args, uintptr_t);
|
|
|
|
int ret = impl->ioctl(dev, cmd, arg);
|
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|