Add DAC user mode interface

Signed-off-by: zhangyalei <zhangyalei7@huawei.com>
This commit is contained in:
zhangyalei 2022-01-29 04:09:03 -08:00
parent 3129e2a524
commit 7aeda33683
6 changed files with 279 additions and 180 deletions

View File

@ -78,6 +78,17 @@ void DacClose(DevHandle handle);
*/
int32_t DacWrite(DevHandle handle, uint32_t channel, uint32_t val);
/**
* @brief Enumerates DAC I/O commands.
*
* @since 1.0
*/
enum DacIoCmd {
DAC_IO_READ = 0,
DAC_IO_OPEN,
DAC_IO_CLOSE,
DAC_IO_WRITE,
};
#ifdef __cplusplus
#if __cplusplus
}

View File

@ -46,12 +46,6 @@ struct DacLockMethod {
void (*unlock)(struct DacDevice *device);
};
enum DacIoCmd {
DAC_IO_READ = 0,
DAC_IO_OPEN,
DAC_IO_CLOSE,
};
int32_t DacDeviceAdd(struct DacDevice *device);
void DacDeviceRemove(struct DacDevice *device);

View File

@ -14,6 +14,7 @@
#include "osal_time.h"
#include "platform_core.h"
#define DAC_HANDLE_SHIFT 0xFF00U
#define HDF_LOG_TAG dac_core_c
struct DacManager {
@ -46,6 +47,21 @@ static const struct DacLockMethod g_dacLockOpsDefault = {
.unlock = DacDeviceUnlockDefault,
};
static inline int32_t DacDeviceLock(struct DacDevice *device)
{
if (device->lockOps == NULL || device->lockOps->lock == NULL) {
return HDF_ERR_NOT_SUPPORT;
}
return device->lockOps->lock(device);
}
static inline void DacDeviceUnlock(struct DacDevice *device)
{
if (device->lockOps != NULL && device->lockOps->unlock != NULL) {
device->lockOps->unlock(device);
}
}
static int32_t DacManagerAddDevice(struct DacDevice *device)
{
int32_t ret;
@ -181,19 +197,34 @@ void DacDevicePut(struct DacDevice *device)
(void)device;
}
static inline int32_t DacDeviceLock(struct DacDevice *device)
static struct DacDevice *DacDeviceOpen(uint32_t number)
{
if (device->lockOps == NULL || device->lockOps->lock == NULL) {
return HDF_ERR_NOT_SUPPORT;
int32_t ret;
struct DacDevice *device = NULL;
device = DacDeviceGet(number);
if (device == NULL) {
HDF_LOGE("%s: Get device failed!", __func__);
return NULL;
}
return device->lockOps->lock(device);
ret = DacDeviceStart(device);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: start device failed!", __func__);
return NULL;
}
return device;
}
static inline void DacDeviceUnlock(struct DacDevice *device)
static void DacDeviceClose(struct DacDevice *device)
{
if (device->lockOps != NULL && device->lockOps->unlock != NULL) {
device->lockOps->unlock(device);
if (device == NULL) {
return;
}
(void)DacDeviceStop(device);
DacDevicePut(device);
}
int32_t DacDeviceWrite(struct DacDevice *device, uint32_t channel, uint32_t val)
@ -273,22 +304,28 @@ static int32_t DacManagerIoOpen(struct HdfSBuf *data, struct HdfSBuf *reply)
uint32_t number;
if (data == NULL || reply == NULL) {
HDF_LOGE("%s: invalid data or reply", __func__);
return HDF_ERR_INVALID_PARAM;
}
if (!HdfSbufReadUint32(data, &number)) {
HDF_LOGE("%s: read number failed!", __func__);
return HDF_ERR_IO;
}
if (number < 0 || number >= DAC_DEVICES_MAX || reply == NULL) {
HDF_LOGE("%s: invalid number %u", __func__, number);
return HDF_ERR_INVALID_PARAM;
}
if (DacDeviceGet(number) == NULL) {
if (DacDeviceOpen(number) == NULL) {
HDF_LOGE("%s: get device %u failed!", __func__, number);
return HDF_ERR_NOT_SUPPORT;
}
if (!HdfSbufWriteUint32(reply, number)) {
number = (uint32_t)(number + DAC_HANDLE_SHIFT);
if (!HdfSbufWriteUint32(reply, (uint32_t)(uintptr_t)number)) {
HDF_LOGE("%s: write number failed!", __func__);
return HDF_ERR_IO;
}
return HDF_SUCCESS;
@ -298,47 +335,89 @@ static int32_t DacManagerIoClose(struct HdfSBuf *data, struct HdfSBuf *reply)
{
uint32_t number;
if (data == NULL || reply == NULL) {
if (data == NULL) {
HDF_LOGE("%s: invalid data", __func__);
return HDF_ERR_INVALID_PARAM;
}
if (!HdfSbufReadUint32(data, &number)) {
HDF_LOGE("%s: read number failed!", __func__);
return HDF_ERR_IO;
}
number = (uint32_t)(number - DAC_HANDLE_SHIFT);
if (number < 0 || number >= DAC_DEVICES_MAX) {
HDF_LOGE("%s: invalid number %u", __func__, number);
return HDF_ERR_INVALID_PARAM;
}
DacDevicePut(DacManagerFindDevice(number));
DacDeviceClose(DacDeviceGet(number));
return HDF_SUCCESS;
}
static int32_t DacManagerIoRead(struct HdfSBuf *data, struct HdfSBuf *reply)
static int32_t DacManagerIoWrite(struct HdfSBuf *data, struct HdfSBuf *reply)
{
(void)data;
(void)reply;
int32_t ret;
uint32_t channel;
uint32_t val;
uint32_t number;
if (data == NULL) {
HDF_LOGE("%s: invalid data", __func__);
return HDF_ERR_INVALID_PARAM;
}
if (!HdfSbufReadUint32(data, &number)) {
HDF_LOGE("%s: read number failed!", __func__);
return HDF_ERR_IO;
}
number = (uint32_t)(number - DAC_HANDLE_SHIFT);
if (number < 0 || number >= DAC_DEVICES_MAX) {
HDF_LOGE("%s: invalid number %u", __func__, number);
return HDF_ERR_INVALID_PARAM;
}
if (!HdfSbufReadUint32(data, &channel)) {
HDF_LOGE("%s: read dac channel failed", __func__);
return HDF_ERR_IO;
}
if (!HdfSbufReadUint32(data, &val)) {
HDF_LOGE("%s: read dac value failed", __func__);
return HDF_ERR_IO;
}
ret = DacDeviceWrite(DacDeviceGet(number), channel, val);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: write dac failed:%d", __func__, ret);
return ret;
}
return HDF_SUCCESS;
}
static int32_t DacManagerDispatch(struct HdfDeviceIoClient *client, int cmd,
struct HdfSBuf *data, struct HdfSBuf *reply)
{
int32_t ret;
switch (cmd) {
case DAC_IO_OPEN:
return DacManagerIoOpen(data, reply);
case DAC_IO_CLOSE:
return DacManagerIoClose(data, reply);
case DAC_IO_READ:
return DacManagerIoRead(data, reply);
case DAC_IO_WRITE:
return DacManagerIoWrite(data, reply);
default:
ret = HDF_ERR_NOT_SUPPORT;
return HDF_ERR_NOT_SUPPORT;
break;
}
return ret;
return HDF_SUCCESS;
}
static int32_t DacManagerBind(struct HdfDeviceObject *device)
{
(void)device;
return HDF_SUCCESS;
}
static int32_t DacManagerInit(struct HdfDeviceObject *device)
{
int32_t ret;
@ -390,6 +469,7 @@ static void DacManagerRelease(struct HdfDeviceObject *device)
struct HdfDriverEntry g_dacManagerEntry = {
.moduleVersion = 1,
.Bind = DacManagerBind,
.Init = DacManagerInit,
.Release = DacManagerRelease,
.moduleName = "HDF_PLATFORM_DAC_MANAGER",

View File

@ -55,6 +55,10 @@ HWTEST_F(HdfLiteDacTest, DacTestWrite001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_DAC_TYPE, DAC_TEST_CMD_WRITE, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, DacTestExecute(DAC_TEST_CMD_WRITE));
printf("%s: exit!\n", __func__);
}
/**
@ -67,6 +71,10 @@ HWTEST_F(HdfLiteDacTest, DacTestMultiThread001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_DAC_TYPE, DAC_TEST_CMD_MULTI_THREAD, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, DacTestExecute(DAC_TEST_CMD_MULTI_THREAD));
printf("%s: exit!\n", __func__);
}
/**
@ -79,4 +87,8 @@ HWTEST_F(HdfLiteDacTest, DacTestReliability001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_DAC_TYPE, DAC_TEST_CMD_RELIABILITY, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, DacTestExecute(DAC_TEST_CMD_RELIABILITY));
printf("%s: exit!\n", __func__);
}

View File

@ -105,7 +105,7 @@ int32_t DacTestWrite(void)
value[i] = 0;
ret = DacWrite(tester->handle, tester->config.channel, value[i]);
if (ret != HDF_SUCCESS || value[i] >= (1U << tester->config.dataWidth)) {
HDF_LOGE("%s: write value invalid:%u, ret:%d", __func__, value[i], ret);
HDF_LOGE("%s: write value failed:%u, ret:%d", __func__, value[i], ret);
return HDF_ERR_IO;
}
}
@ -119,6 +119,7 @@ static int DacTestThreadFunc(void *param)
uint32_t val;
uint32_t i;
int32_t ret;
tester = DacTesterGet();
if (tester == NULL) {
HDF_LOGE("%s: get tester failed", __func__);
@ -130,6 +131,7 @@ static int DacTestThreadFunc(void *param)
ret = DacWrite(tester->handle, tester->config.channel, val);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: DacWrite failed, ret:%d", __func__, ret);
*((int32_t *)param) = 1;
return HDF_ERR_IO;
}
}