From 91b4f72ce13472297958f1d114a30dee16076ece Mon Sep 17 00:00:00 2001 From: lizhun Date: Wed, 28 Jul 2021 11:31:09 +0800 Subject: [PATCH 1/4] gpio_adapter patch --- support/platform/include/gpio_core.h | 8 ++- support/platform/include/plat_log.h | 2 +- support/platform/src/gpio_core.c | 77 ++++++++++++++++++---------- support/platform/src/gpio_if.c | 16 +++--- 4 files changed, 66 insertions(+), 37 deletions(-) diff --git a/support/platform/include/gpio_core.h b/support/platform/include/gpio_core.h index 6e1a0dc44..4f8fbdd6f 100644 --- a/support/platform/include/gpio_core.h +++ b/support/platform/include/gpio_core.h @@ -25,7 +25,7 @@ struct GpioCntlr; struct GpioMethod; struct GpioInfo; -#define GPIO_NUM_DEFAULT 200 +#define GPIO_NUM_MAX 0xFFFF /** * @brief Defines the struct which represent a hardware GPIO controller. @@ -138,6 +138,12 @@ void GpioCntlrIrqCallback(struct GpioCntlr *cntlr, uint16_t local); struct GpioCntlr *GpioGetCntlr(uint16_t gpio); +static inline uint16_t GpioToLocal(uint16_t gpio) +{ + struct GpioCntlr *cntlr = GpioGetCntlr(gpio); + return (cntlr == NULL) ? gpio : (gpio - cntlr->start); +} + static inline uint16_t GpioGetLocalNumber(struct GpioCntlr *cntlr, uint16_t gpio) { return (cntlr == NULL) ? gpio : (gpio - cntlr->start); diff --git a/support/platform/include/plat_log.h b/support/platform/include/plat_log.h index bc2d0671c..d3b4c6f29 100644 --- a/support/platform/include/plat_log.h +++ b/support/platform/include/plat_log.h @@ -11,7 +11,7 @@ #include "hdf_log.h" -#if defined(__LITEOS__) +#if defined(__LITEOS__) || defined(__KERNEL__) #define PLAT_LOGV(fmt, arg...) #else #define PLAT_LOGV(fmt, arg...) HDF_LOGV(fmt, ##arg) diff --git a/support/platform/src/gpio_core.c b/support/platform/src/gpio_core.c index 7224aeb2b..fc502b1b1 100644 --- a/support/platform/src/gpio_core.c +++ b/support/platform/src/gpio_core.c @@ -56,9 +56,50 @@ static void GpioCntlrListPut(void) (void)OsalSpinUnlockIrqRestore(&g_listLock, &g_irqFlags); } +static uint16_t GpioCntlrQueryStart(struct GpioCntlr *cntlr, struct DListHead *list) +{ + uint16_t freeStart; + uint16_t freeCount; + struct GpioCntlr *iterLast = NULL; + struct GpioCntlr *iterCur = NULL; + struct GpioCntlr *tmp = NULL; + + DLIST_FOR_EACH_ENTRY_SAFE(iterCur, tmp, list, struct GpioCntlr, list) { + if (iterLast == NULL) { + freeStart = 0; + freeCount = iterCur->start; + } else { + freeStart = iterLast->start + iterLast->count; + freeCount = iterCur->start - freeStart; + } + + if (cntlr->start < freeStart) { + HDF_LOGE("GpioCntlrQueryStart: start:%u not available(freeStart:%u, freeCount:%u)", + cntlr->start, freeStart, freeCount); + return GPIO_NUM_MAX; + } + + if ((cntlr->start + cntlr->count) <= (freeStart + freeCount)) { + return freeStart; + } + + iterLast = iterCur; + } + if (iterLast == NULL) { // empty list + return cntlr->start; + } + if (cntlr->start >= (iterLast->start + iterLast->count)) { + return iterLast->start + iterLast->count; + } + HDF_LOGE("GpioCntlrQueryStart: start:%u not available(lastStart:%u, lastCount:%u)", + cntlr->start, iterLast->start, iterLast->count); + return GPIO_NUM_MAX; +} + + int32_t GpioCntlrAdd(struct GpioCntlr *cntlr) { - struct GpioCntlr *last = NULL; + uint16_t start; struct DListHead *list = NULL; if (cntlr == NULL) { @@ -90,12 +131,12 @@ int32_t GpioCntlrAdd(struct GpioCntlr *cntlr) OsalSpinInit(&cntlr->spin); list = GpioCntlrListGet(); - if (DListIsEmpty(list)) { - cntlr->start = 0; - } else { - last = DLIST_LAST_ENTRY(list, struct GpioCntlr, list); - cntlr->start = last->start + last->count; - } + if ((start = GpioCntlrQueryStart(cntlr, list)) >= GPIO_NUM_MAX) { + HDF_LOGE("GpioCntlrAdd: query range for start:%d fail:%d", cntlr->start, start); + GpioCntlrListPut(); + return HDF_ERR_INVALID_PARAM; + } + cntlr->start = start; DListHeadInit(&cntlr->list); DListInsertTail(&cntlr->list, list); GpioCntlrListPut(); @@ -219,7 +260,6 @@ static int32_t GpioIrqBridgeFunc(uint16_t local, void *data) return HDF_SUCCESS; } -#ifndef __KERNEL__ static int GpioIrqThreadWorker(void *data) { int32_t ret; @@ -234,7 +274,7 @@ static int GpioIrqThreadWorker(void *data) break; } PLAT_LOGV("GpioIrqThreadWorker: enter! gpio:%u-%u", bridge->cntlr->start, bridge->local); - (void)bridge->func(bridge->local, bridge->data); + (void)bridge->func(bridge->local + bridge->cntlr->start, bridge->data); } /* it's the bridge struct we create before, so release it ourself */ (void)OsalSemDestroy(&bridge->sem); @@ -303,21 +343,9 @@ __ERR_FORMAT_NAME: OsalMemFree(bridge); return NULL; } -#else -static struct GpioIrqBridge *GpioIrqBridgeCreate(struct GpioCntlr *cntlr, - uint16_t local, GpioIrqFunc func, void *arg) -{ - (void)cntlr; - (void)local; - (void)func; - (void)arg; - return NULL; -} -#endif static void GpioIrqBridgeDestroy(struct GpioIrqBridge *bridge) { -#ifndef __KERNEL__ uint32_t flags; (void)OsalSpinLockIrqSave(&bridge->spin, &flags); if (!bridge->stop) { @@ -325,9 +353,6 @@ static void GpioIrqBridgeDestroy(struct GpioIrqBridge *bridge) (void)OsalSemPost(&bridge->sem); } (void)OsalSpinUnlockIrqRestore(&bridge->spin, &flags); -#else - (void)bridge; -#endif } void GpioCntlrIrqCallback(struct GpioCntlr *cntlr, uint16_t local) @@ -342,7 +367,7 @@ void GpioCntlrIrqCallback(struct GpioCntlr *cntlr, uint16_t local) HDF_LOGW("GpioCntlrIrqCallback: ginfo or irqFunc is NULL!"); } } else { - HDF_LOGW("GpioCntlrIrqCallback: invalid cntlr(ginfos) or loal num!"); + HDF_LOGW("GpioCntlrIrqCallback: invalid cntlr(ginfos) or loal num:%u!", local); } } @@ -372,11 +397,9 @@ int32_t GpioCntlrSetIrq(struct GpioCntlr *cntlr, uint16_t local, uint16_t mode, theData = bridge; theFunc = GpioIrqBridgeFunc; } -#ifndef __KERNEL__ if (bridge == NULL) { return HDF_FAILURE; } -#endif } (void)OsalSpinLockIrqSave(&cntlr->spin, &flags); diff --git a/support/platform/src/gpio_if.c b/support/platform/src/gpio_if.c index 444fdfe37..eca819fc0 100644 --- a/support/platform/src/gpio_if.c +++ b/support/platform/src/gpio_if.c @@ -16,40 +16,40 @@ int32_t GpioRead(uint16_t gpio, uint16_t *val) { - return GpioCntlrRead(GpioGetCntlr(gpio), gpio, val); + return GpioCntlrRead(GpioGetCntlr(gpio), GpioToLocal(gpio), val); } int32_t GpioWrite(uint16_t gpio, uint16_t val) { - return GpioCntlrWrite(GpioGetCntlr(gpio), gpio, val); + return GpioCntlrWrite(GpioGetCntlr(gpio), GpioToLocal(gpio), val); } int32_t GpioSetDir(uint16_t gpio, uint16_t dir) { - return GpioCntlrSetDir(GpioGetCntlr(gpio), gpio, dir); + return GpioCntlrSetDir(GpioGetCntlr(gpio), GpioToLocal(gpio), dir); } int32_t GpioGetDir(uint16_t gpio, uint16_t *dir) { - return GpioCntlrGetDir(GpioGetCntlr(gpio), gpio, dir); + return GpioCntlrGetDir(GpioGetCntlr(gpio), GpioToLocal(gpio), dir); } int32_t GpioSetIrq(uint16_t gpio, uint16_t mode, GpioIrqFunc func, void *arg) { - return GpioCntlrSetIrq(GpioGetCntlr(gpio), gpio, mode, func, arg); + return GpioCntlrSetIrq(GpioGetCntlr(gpio), GpioToLocal(gpio), mode, func, arg); } int32_t GpioUnSetIrq(uint16_t gpio) { - return GpioCntlrUnsetIrq(GpioGetCntlr(gpio), gpio); + return GpioCntlrUnsetIrq(GpioGetCntlr(gpio), GpioToLocal(gpio)); } int32_t GpioEnableIrq(uint16_t gpio) { - return GpioCntlrEnableIrq(GpioGetCntlr(gpio), gpio); + return GpioCntlrEnableIrq(GpioGetCntlr(gpio), GpioToLocal(gpio)); } int32_t GpioDisableIrq(uint16_t gpio) { - return GpioCntlrDisableIrq(GpioGetCntlr(gpio), gpio); + return GpioCntlrDisableIrq(GpioGetCntlr(gpio), GpioToLocal(gpio)); } -- Gitee From 2645b485a51b36629a99e92939134053de351910 Mon Sep 17 00:00:00 2001 From: yinshuqing Date: Thu, 22 Jul 2021 21:30:22 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=91=8A=E8=AD=A6platform=5Fdevice=20platform=5Fmanager.c=20mm?= =?UTF-8?q?c=5Fif.c=20mmc=5Fprotocol.c=20uart=5Fcore.c=20i2c=5Ftest.c=20sp?= =?UTF-8?q?i=5Ftest.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yinshuqing --- support/platform/src/common/platform_device.c | 11 ++++++----- support/platform/src/common/platform_manager.c | 4 ++-- support/platform/src/mmc/mmc_if.c | 1 - support/platform/src/mmc/mmc_protocol.c | 3 ++- support/platform/src/uart_core.c | 1 + test/unittest/platform/common/i2c_test.c | 6 ++++++ test/unittest/platform/common/spi_test.c | 2 +- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/support/platform/src/common/platform_device.c b/support/platform/src/common/platform_device.c index 05ae7ea72..62050c05a 100644 --- a/support/platform/src/common/platform_device.c +++ b/support/platform/src/common/platform_device.c @@ -38,7 +38,7 @@ static void PlatformDeviceOnLastPut(struct HdfSRef *sref) device->ready = false; DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &device->notifiers, struct PlatformNotifierNode, node) { - if (pos != NULL && pos->notifier != NULL && pos->notifier->handle != NULL) { + if (pos!=NULL && pos->notifier != NULL && pos->notifier->handle != NULL) { pos->notifier->handle(device, PLAT_EVENT_DEAD, pos->notifier->data); } } @@ -153,14 +153,15 @@ static void PlatformDeviceRemoveNotifier(struct PlatformDevice *device, struct P (void)OsalSpinLock(&device->spin); DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &device->notifiers, struct PlatformNotifierNode, node) { - if (pos != NULL && pos->notifier != NULL) { + if (pos->notifier != NULL) { /* if notifier not set, we remove all the notifier nodes. */ - if (notifier == NULL || notifier == pos->notifier) { + if (notifier == pos->notifier) { DListRemove(&pos->node); OsalMemFree(pos); - } - if (notifier == pos->notifier) { break; + }else if (notifier == NULL) { + DListRemove(&pos->node); + OsalMemFree(pos); } } } diff --git a/support/platform/src/common/platform_manager.c b/support/platform/src/common/platform_manager.c index 0f3ccf662..2dbc222d6 100644 --- a/support/platform/src/common/platform_manager.c +++ b/support/platform/src/common/platform_manager.c @@ -78,7 +78,7 @@ int32_t PlatformManagerAddDevice(struct PlatformManager *manager, struct Platfor (void)OsalSpinLock(&manager->spin); DLIST_FOR_EACH_ENTRY(tmp, &manager->devices, struct PlatformDevice, node) { - if (tmp != NULL && tmp->magic == device->magic) { + if (tmp!=NULL && tmp->magic == device->magic) { repeatId = true; HDF_LOGE("PlatformManagerAddDevice: repeated magic:%u!", device->magic); break; @@ -130,7 +130,7 @@ struct PlatformDevice *PlatformManagerFindDevice(struct PlatformManager *manager (void)OsalSpinLock(&manager->spin); DLIST_FOR_EACH_ENTRY(tmp, &manager->devices, struct PlatformDevice, node) { - if (tmp != NULL && match(tmp, data)) { + if (tmp!=NULL && match(tmp, data)) { pdevice = PlatformDeviceGet(tmp); } } diff --git a/support/platform/src/mmc/mmc_if.c b/support/platform/src/mmc/mmc_if.c index 8b37d0d79..dba2d503a 100644 --- a/support/platform/src/mmc/mmc_if.c +++ b/support/platform/src/mmc/mmc_if.c @@ -29,7 +29,6 @@ static void *MmcCntlrObjGetByNumber(int16_t id) char *serviceName = NULL; if (id < 0) { - HDF_LOGE("MmcCntlrObjGetByNumber: invalid id:%d", id); return NULL; } serviceName = OsalMemCalloc(MMC_SVC_NAME_LEN + 1); diff --git a/support/platform/src/mmc/mmc_protocol.c b/support/platform/src/mmc/mmc_protocol.c index 304c3e127..a2ee8cfcb 100644 --- a/support/platform/src/mmc/mmc_protocol.c +++ b/support/platform/src/mmc/mmc_protocol.c @@ -3494,7 +3494,7 @@ static void SdioDecodeCisTplManfId(struct MmcCntlr *cntlr, struct SdioFunction * */ vendorId = (tuple->tplBody[1] << BITS_PER_BYTE) | tuple->tplBody[0]; deviceId = (tuple->tplBody[3] << BITS_PER_BYTE) | tuple->tplBody[2]; - HDF_LOGD("Sdio vendorId = 0x%x, deviceId = 0x%x.", vendorId, deviceId); + /* function CISTPL_MANFID. */ if (function != NULL) { function->deviceId = deviceId; @@ -3838,6 +3838,7 @@ static int32_t SdioAddFunctions(struct MmcCntlr *cntlr, uint32_t funcs) } ret = SdioReadFbr(cntlr, function); if (ret != HDF_SUCCESS) { + SdioDeleteFunction(function); return ret; } ret = SdioReadCis(cntlr, function); diff --git a/support/platform/src/uart_core.c b/support/platform/src/uart_core.c index 71cfcb050..caf03d1aa 100644 --- a/support/platform/src/uart_core.c +++ b/support/platform/src/uart_core.c @@ -75,6 +75,7 @@ static int32_t UartUserRead(struct UartHost *host, struct HdfSBuf *reply) } if (!HdfSbufWriteBuffer(reply, buf, len)) { HDF_LOGE("%s: sbuf write buffer failed", __func__); + OsalMemFree(buf); return HDF_ERR_IO; } OsalMemFree(buf); diff --git a/test/unittest/platform/common/i2c_test.c b/test/unittest/platform/common/i2c_test.c index cba586b29..2c6a9ed04 100644 --- a/test/unittest/platform/common/i2c_test.c +++ b/test/unittest/platform/common/i2c_test.c @@ -75,6 +75,7 @@ static int32_t I2cTestGetConfig(struct I2cTestConfig *config) HdfSBufRecycle(reply); HDF_LOGD("I2cTestGetConfig: exit!"); + HdfIoServiceRecycle(service); return HDF_SUCCESS; } @@ -260,6 +261,7 @@ int32_t I2cTestMultiThread(void) ret = OsalThreadCreate(&thread2, (OsalThreadEntry)I2cTestThreadFunc, (void *)&count2); if (ret != HDF_SUCCESS) { + (void)OsalThreadDestroy(&thread1); HDF_LOGE("create test thread1 fail:%d", ret); return HDF_FAILURE; } @@ -271,12 +273,16 @@ int32_t I2cTestMultiThread(void) ret = OsalThreadStart(&thread1, &cfg1); if (ret != HDF_SUCCESS) { + (void)OsalThreadDestroy(&thread1); + (void)OsalThreadDestroy(&thread2); HDF_LOGE("start test thread1 fail:%d", ret); return HDF_FAILURE; } ret = OsalThreadStart(&thread2, &cfg2); if (ret != HDF_SUCCESS) { + (void)OsalThreadDestroy(&thread1); + (void)OsalThreadDestroy(&thread2); HDF_LOGE("start test thread2 fail:%d", ret); return HDF_FAILURE; } diff --git a/test/unittest/platform/common/spi_test.c b/test/unittest/platform/common/spi_test.c index 671d3fd4c..d1603ac08 100644 --- a/test/unittest/platform/common/spi_test.c +++ b/test/unittest/platform/common/spi_test.c @@ -175,7 +175,7 @@ static int32_t SpiSetDmaIntMsg(struct SpiMsg *msg, uint32_t len) return HDF_ERR_MALLOC_FAIL; } rbuf = (uint8_t *)OsalMemAllocAlign(DMA_ALIGN_SIZE, len); - if (wbuf == NULL) { + if (rbuf == NULL) { OsalMemFree(wbuf); return HDF_ERR_MALLOC_FAIL; } -- Gitee From 5fd43397225ec6fe45e4a47978227024713e943f Mon Sep 17 00:00:00 2001 From: yinshuqing Date: Fri, 23 Jul 2021 10:04:01 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86platform=5Fdevic?= =?UTF-8?q?e.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yinshuqing --- support/platform/src/common/platform_device.c | 9 ++++----- support/platform/src/common/platform_manager.c | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) mode change 100644 => 100755 support/platform/src/common/platform_device.c diff --git a/support/platform/src/common/platform_device.c b/support/platform/src/common/platform_device.c old mode 100644 new mode 100755 index 62050c05a..734845563 --- a/support/platform/src/common/platform_device.c +++ b/support/platform/src/common/platform_device.c @@ -38,7 +38,7 @@ static void PlatformDeviceOnLastPut(struct HdfSRef *sref) device->ready = false; DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &device->notifiers, struct PlatformNotifierNode, node) { - if (pos!=NULL && pos->notifier != NULL && pos->notifier->handle != NULL) { + if (pos->notifier != NULL && pos->notifier->handle != NULL) { pos->notifier->handle(device, PLAT_EVENT_DEAD, pos->notifier->data); } } @@ -155,13 +155,12 @@ static void PlatformDeviceRemoveNotifier(struct PlatformDevice *device, struct P DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &device->notifiers, struct PlatformNotifierNode, node) { if (pos->notifier != NULL) { /* if notifier not set, we remove all the notifier nodes. */ - if (notifier == pos->notifier) { + if (notifier == NULL || notifier == pos->notifier) { DListRemove(&pos->node); OsalMemFree(pos); + } + if (notifier == pos->notifier) { break; - }else if (notifier == NULL) { - DListRemove(&pos->node); - OsalMemFree(pos); } } } diff --git a/support/platform/src/common/platform_manager.c b/support/platform/src/common/platform_manager.c index 2dbc222d6..0f3ccf662 100644 --- a/support/platform/src/common/platform_manager.c +++ b/support/platform/src/common/platform_manager.c @@ -78,7 +78,7 @@ int32_t PlatformManagerAddDevice(struct PlatformManager *manager, struct Platfor (void)OsalSpinLock(&manager->spin); DLIST_FOR_EACH_ENTRY(tmp, &manager->devices, struct PlatformDevice, node) { - if (tmp!=NULL && tmp->magic == device->magic) { + if (tmp != NULL && tmp->magic == device->magic) { repeatId = true; HDF_LOGE("PlatformManagerAddDevice: repeated magic:%u!", device->magic); break; @@ -130,7 +130,7 @@ struct PlatformDevice *PlatformManagerFindDevice(struct PlatformManager *manager (void)OsalSpinLock(&manager->spin); DLIST_FOR_EACH_ENTRY(tmp, &manager->devices, struct PlatformDevice, node) { - if (tmp!=NULL && match(tmp, data)) { + if (tmp != NULL && match(tmp, data)) { pdevice = PlatformDeviceGet(tmp); } } -- Gitee From 9297600a644008ebc9495d841136a85f0350f830 Mon Sep 17 00:00:00 2001 From: yinshuqing Date: Fri, 23 Jul 2021 14:11:47 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86platform=5Fdevic?= =?UTF-8?q?e.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yinshuqing --- support/platform/include/gpio_core.h | 8 +- support/platform/include/plat_log.h | 2 +- support/platform/src/common/platform_device.c | 9 ++- support/platform/src/gpio_core.c | 77 ++++++++++++------- support/platform/src/gpio_if.c | 16 ++-- 5 files changed, 71 insertions(+), 41 deletions(-) diff --git a/support/platform/include/gpio_core.h b/support/platform/include/gpio_core.h index 6e1a0dc44..4f8fbdd6f 100644 --- a/support/platform/include/gpio_core.h +++ b/support/platform/include/gpio_core.h @@ -25,7 +25,7 @@ struct GpioCntlr; struct GpioMethod; struct GpioInfo; -#define GPIO_NUM_DEFAULT 200 +#define GPIO_NUM_MAX 0xFFFF /** * @brief Defines the struct which represent a hardware GPIO controller. @@ -138,6 +138,12 @@ void GpioCntlrIrqCallback(struct GpioCntlr *cntlr, uint16_t local); struct GpioCntlr *GpioGetCntlr(uint16_t gpio); +static inline uint16_t GpioToLocal(uint16_t gpio) +{ + struct GpioCntlr *cntlr = GpioGetCntlr(gpio); + return (cntlr == NULL) ? gpio : (gpio - cntlr->start); +} + static inline uint16_t GpioGetLocalNumber(struct GpioCntlr *cntlr, uint16_t gpio) { return (cntlr == NULL) ? gpio : (gpio - cntlr->start); diff --git a/support/platform/include/plat_log.h b/support/platform/include/plat_log.h index bc2d0671c..d3b4c6f29 100644 --- a/support/platform/include/plat_log.h +++ b/support/platform/include/plat_log.h @@ -11,7 +11,7 @@ #include "hdf_log.h" -#if defined(__LITEOS__) +#if defined(__LITEOS__) || defined(__KERNEL__) #define PLAT_LOGV(fmt, arg...) #else #define PLAT_LOGV(fmt, arg...) HDF_LOGV(fmt, ##arg) diff --git a/support/platform/src/common/platform_device.c b/support/platform/src/common/platform_device.c index 734845563..bd8e214a7 100755 --- a/support/platform/src/common/platform_device.c +++ b/support/platform/src/common/platform_device.c @@ -155,12 +155,13 @@ static void PlatformDeviceRemoveNotifier(struct PlatformDevice *device, struct P DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &device->notifiers, struct PlatformNotifierNode, node) { if (pos->notifier != NULL) { /* if notifier not set, we remove all the notifier nodes. */ - if (notifier == NULL || notifier == pos->notifier) { + if (notifier == pos->notifier) { + DListRemove(&pos->node); + OsalMemFree(pos); + break; + } else if (notifier == NULL) { DListRemove(&pos->node); OsalMemFree(pos); - } - if (notifier == pos->notifier) { - break; } } } diff --git a/support/platform/src/gpio_core.c b/support/platform/src/gpio_core.c index 7224aeb2b..fc502b1b1 100644 --- a/support/platform/src/gpio_core.c +++ b/support/platform/src/gpio_core.c @@ -56,9 +56,50 @@ static void GpioCntlrListPut(void) (void)OsalSpinUnlockIrqRestore(&g_listLock, &g_irqFlags); } +static uint16_t GpioCntlrQueryStart(struct GpioCntlr *cntlr, struct DListHead *list) +{ + uint16_t freeStart; + uint16_t freeCount; + struct GpioCntlr *iterLast = NULL; + struct GpioCntlr *iterCur = NULL; + struct GpioCntlr *tmp = NULL; + + DLIST_FOR_EACH_ENTRY_SAFE(iterCur, tmp, list, struct GpioCntlr, list) { + if (iterLast == NULL) { + freeStart = 0; + freeCount = iterCur->start; + } else { + freeStart = iterLast->start + iterLast->count; + freeCount = iterCur->start - freeStart; + } + + if (cntlr->start < freeStart) { + HDF_LOGE("GpioCntlrQueryStart: start:%u not available(freeStart:%u, freeCount:%u)", + cntlr->start, freeStart, freeCount); + return GPIO_NUM_MAX; + } + + if ((cntlr->start + cntlr->count) <= (freeStart + freeCount)) { + return freeStart; + } + + iterLast = iterCur; + } + if (iterLast == NULL) { // empty list + return cntlr->start; + } + if (cntlr->start >= (iterLast->start + iterLast->count)) { + return iterLast->start + iterLast->count; + } + HDF_LOGE("GpioCntlrQueryStart: start:%u not available(lastStart:%u, lastCount:%u)", + cntlr->start, iterLast->start, iterLast->count); + return GPIO_NUM_MAX; +} + + int32_t GpioCntlrAdd(struct GpioCntlr *cntlr) { - struct GpioCntlr *last = NULL; + uint16_t start; struct DListHead *list = NULL; if (cntlr == NULL) { @@ -90,12 +131,12 @@ int32_t GpioCntlrAdd(struct GpioCntlr *cntlr) OsalSpinInit(&cntlr->spin); list = GpioCntlrListGet(); - if (DListIsEmpty(list)) { - cntlr->start = 0; - } else { - last = DLIST_LAST_ENTRY(list, struct GpioCntlr, list); - cntlr->start = last->start + last->count; - } + if ((start = GpioCntlrQueryStart(cntlr, list)) >= GPIO_NUM_MAX) { + HDF_LOGE("GpioCntlrAdd: query range for start:%d fail:%d", cntlr->start, start); + GpioCntlrListPut(); + return HDF_ERR_INVALID_PARAM; + } + cntlr->start = start; DListHeadInit(&cntlr->list); DListInsertTail(&cntlr->list, list); GpioCntlrListPut(); @@ -219,7 +260,6 @@ static int32_t GpioIrqBridgeFunc(uint16_t local, void *data) return HDF_SUCCESS; } -#ifndef __KERNEL__ static int GpioIrqThreadWorker(void *data) { int32_t ret; @@ -234,7 +274,7 @@ static int GpioIrqThreadWorker(void *data) break; } PLAT_LOGV("GpioIrqThreadWorker: enter! gpio:%u-%u", bridge->cntlr->start, bridge->local); - (void)bridge->func(bridge->local, bridge->data); + (void)bridge->func(bridge->local + bridge->cntlr->start, bridge->data); } /* it's the bridge struct we create before, so release it ourself */ (void)OsalSemDestroy(&bridge->sem); @@ -303,21 +343,9 @@ __ERR_FORMAT_NAME: OsalMemFree(bridge); return NULL; } -#else -static struct GpioIrqBridge *GpioIrqBridgeCreate(struct GpioCntlr *cntlr, - uint16_t local, GpioIrqFunc func, void *arg) -{ - (void)cntlr; - (void)local; - (void)func; - (void)arg; - return NULL; -} -#endif static void GpioIrqBridgeDestroy(struct GpioIrqBridge *bridge) { -#ifndef __KERNEL__ uint32_t flags; (void)OsalSpinLockIrqSave(&bridge->spin, &flags); if (!bridge->stop) { @@ -325,9 +353,6 @@ static void GpioIrqBridgeDestroy(struct GpioIrqBridge *bridge) (void)OsalSemPost(&bridge->sem); } (void)OsalSpinUnlockIrqRestore(&bridge->spin, &flags); -#else - (void)bridge; -#endif } void GpioCntlrIrqCallback(struct GpioCntlr *cntlr, uint16_t local) @@ -342,7 +367,7 @@ void GpioCntlrIrqCallback(struct GpioCntlr *cntlr, uint16_t local) HDF_LOGW("GpioCntlrIrqCallback: ginfo or irqFunc is NULL!"); } } else { - HDF_LOGW("GpioCntlrIrqCallback: invalid cntlr(ginfos) or loal num!"); + HDF_LOGW("GpioCntlrIrqCallback: invalid cntlr(ginfos) or loal num:%u!", local); } } @@ -372,11 +397,9 @@ int32_t GpioCntlrSetIrq(struct GpioCntlr *cntlr, uint16_t local, uint16_t mode, theData = bridge; theFunc = GpioIrqBridgeFunc; } -#ifndef __KERNEL__ if (bridge == NULL) { return HDF_FAILURE; } -#endif } (void)OsalSpinLockIrqSave(&cntlr->spin, &flags); diff --git a/support/platform/src/gpio_if.c b/support/platform/src/gpio_if.c index 444fdfe37..eca819fc0 100644 --- a/support/platform/src/gpio_if.c +++ b/support/platform/src/gpio_if.c @@ -16,40 +16,40 @@ int32_t GpioRead(uint16_t gpio, uint16_t *val) { - return GpioCntlrRead(GpioGetCntlr(gpio), gpio, val); + return GpioCntlrRead(GpioGetCntlr(gpio), GpioToLocal(gpio), val); } int32_t GpioWrite(uint16_t gpio, uint16_t val) { - return GpioCntlrWrite(GpioGetCntlr(gpio), gpio, val); + return GpioCntlrWrite(GpioGetCntlr(gpio), GpioToLocal(gpio), val); } int32_t GpioSetDir(uint16_t gpio, uint16_t dir) { - return GpioCntlrSetDir(GpioGetCntlr(gpio), gpio, dir); + return GpioCntlrSetDir(GpioGetCntlr(gpio), GpioToLocal(gpio), dir); } int32_t GpioGetDir(uint16_t gpio, uint16_t *dir) { - return GpioCntlrGetDir(GpioGetCntlr(gpio), gpio, dir); + return GpioCntlrGetDir(GpioGetCntlr(gpio), GpioToLocal(gpio), dir); } int32_t GpioSetIrq(uint16_t gpio, uint16_t mode, GpioIrqFunc func, void *arg) { - return GpioCntlrSetIrq(GpioGetCntlr(gpio), gpio, mode, func, arg); + return GpioCntlrSetIrq(GpioGetCntlr(gpio), GpioToLocal(gpio), mode, func, arg); } int32_t GpioUnSetIrq(uint16_t gpio) { - return GpioCntlrUnsetIrq(GpioGetCntlr(gpio), gpio); + return GpioCntlrUnsetIrq(GpioGetCntlr(gpio), GpioToLocal(gpio)); } int32_t GpioEnableIrq(uint16_t gpio) { - return GpioCntlrEnableIrq(GpioGetCntlr(gpio), gpio); + return GpioCntlrEnableIrq(GpioGetCntlr(gpio), GpioToLocal(gpio)); } int32_t GpioDisableIrq(uint16_t gpio) { - return GpioCntlrDisableIrq(GpioGetCntlr(gpio), gpio); + return GpioCntlrDisableIrq(GpioGetCntlr(gpio), GpioToLocal(gpio)); } -- Gitee