WLAN开发实例

    1. #include "gpio_if.h"
    2. #include "hdf_wifi_core.h"
    3. #include "hdf_device_desc.h"
    4. #include "hdf_wifi_product.h"
    5. #include "hdf_log.h"
    6. #include "hdf_wifi_cmd.h"
    7. #include "osal_mem.h"
    8. #include "osal_time.h"
    9. #include "hdf_wifi_config.h"
    10. struct HdfWifiProductData g_hdfWifiProductData;
    11. /* 声明芯片驱动 */
    12. HDF_WIFI_CHIP_DECLARE(hisi);
    13. int32_t Hi3881Deinit(struct HdfWifiChipData *chipData);
    14. int32_t Hi3881Init(struct HdfWifiChipData *chipData, const struct HdfConfigWifiChip *chipConfig);
    15. HDF_WIFI_CHIP_DRIVER(hisi, Hi3881Init, Hi3881Deinit);
    16. struct HdfWifiChipData *g_wifiChips[] = {
    17. HDF_WIFI_CHIP(hisi),
    18. };
    19. /* 声明芯片驱动 */
    20. static const struct HdfConfigWifiBoard *HdfWifiGetBoardConfig(void)
    21. {
    22. struct HdfConfigWifiRoot *rootConfig = HdfWifiGetModuleConfigRoot();
    23. return &rootConfig->wifiConfig.board;
    24. }
    25. int32_t HdfWifiGetBusIdx(void)
    26. {
    27. const struct HdfConfigWifiBoard *board = HdfWifiGetBoardConfig();
    28. if (board != NULL) {
    29. return board->busIdx;
    30. }
    31. return -1;
    32. }
    33. static void HdfWifiChipReset(void)
    34. {
    35. const struct HdfConfigWifiBoard *board = HdfWifiGetBoardConfig();
    36. uint16_t gpio;
    37. int32_t ret;
    38. if (board != NULL && board->busType == BUS_SDIO) {
    39. /* set dir input */
    40. gpio = board->reset[0] * board->gpioArgs[1] + board->reset[1];
    41. ret = GpioSetDir(gpio, 1);
    42. if (ret != HDF_SUCCESS) {
    43. HDF_LOGE("%s: set dir fail! ret:%d\n", __func__, ret);
    44. return;
    45. }
    46. /* power off */
    47. ret = GpioWrite(gpio, 0);
    48. if (ret != HDF_SUCCESS) {
    49. HDF_LOGE("%s: val:0 write fail! ret:%d\n", __func__, ret);
    50. return;
    51. }
    52. OsalMSleep(0x10);
    53. /* power on */
    54. ret = GpioWrite(gpio, 1);
    55. if (ret != HDF_SUCCESS) {
    56. HDF_LOGE("%s: val:1 write fail! ret:%d\n", __func__, ret);
    57. return;
    58. }
    59. OsalMSleep(0x20);
    60. }
    61. return;
    62. }
    63. static int32_t HdfWifiProductInit(struct HdfDeviceObject *device)
    64. {
    65. int32_t ret;
    66. ret = HdfParseWifiConfig(device->property);
    67. if (ret != HDF_SUCCESS) {
    68. HDF_LOGE("%s:ParseWifiConfig error ret=%d", __func__, ret);
    69. return ret;
    70. }
    71. /* reset chip firstly */
    72. HdfWifiChipReset();
    73. return HDF_SUCCESS;
    74. /* WifiModule初始化 */
    75. static int16_t HdfWifiModuleInit(const struct HdfConfigWifiModuleConfig *config)
    76. {
    77. if (config == NULL) {
    78. return HDF_FAILURE;
    79. }
    80. /* 创建Module */
    81. struct WifiModule *module = WifiModuleCreate(config);
    82. if (module == NULL) {
    83. return HDF_FAILURE;
    84. }
    85. g_hdfWifiProductData.module = module;
    86. HDF_LOGD("%s:hdf wifi module init succ", __func__);
    87. return HDF_SUCCESS;
    88. }
    89. /* 通过芯片的名称获取相应的芯片驱动 */
    90. static struct HdfWifiChipData *HdfWifiFindChipDriverByName(const char *chipName)
    91. {
    92. if (chipName == NULL) {
    93. return NULL;
    94. }
    95. bool found = FALSE;
    96. struct HdfWifiChipData *chipDriver = NULL;
    97. int32_t chipNum = sizeof(g_wifiChips) / sizeof(g_wifiChips[0]);
    98. /* 循环遍历所配置的芯片,返回匹配的芯片驱动 */
    99. for (int32_t i = 0; i < chipNum; i++) {
    100. chipDriver = g_wifiChips[i];
    101. if (chipDriver != NULL && !strcmp(chipDriver->name, chipName)) {
    102. found = TRUE;
    103. HDF_LOGI("%s: find chip %s", __func__, chipName);
    104. break;
    105. }
    106. }
    107. return found ? chipDriver : NULL;
    108. }
    109. /* 芯片初始化 */
    110. static int16_t HdfWifiChipInit(struct WifiModule *module, const struct HdfConfigWifiChip *chipConfig)
    111. {
    112. int32_t ret;
    113. if (module == NULL) {
    114. HDF_LOGE("%s: module is NULL", __func__);
    115. return HDF_FAILURE;
    116. }
    117. /* 通过芯片的名称获取相应的芯片驱动 */
    118. struct HdfWifiChipData *chipDriver = HdfWifiFindChipDriverByName(chipConfig->chipName);
    119. if (chipDriver == NULL) {
    120. HDF_LOGI("%s: chip driver %s not supported", __func__, chipConfig->chipName);
    121. return HDF_DEV_ERR_OP;
    122. }
    123. if (chipDriver->init == NULL) {
    124. HDF_LOGI("%s: chip driver %s not 'init' api", __func__, chipConfig->chipName);
    125. return HDF_DEV_ERR_OP;
    126. }
    127. /* 初始化芯片 */
    128. ret = chipDriver->init(chipDriver, chipConfig);
    129. if (ret != HDF_SUCCESS) {
    130. HDF_LOGE("%s:init chip %s error ret=%d", __func__, chipConfig->chipName, ret);
    131. return HDF_DEV_ERR_OP;
    132. }
    133. /* 将芯片和feature进行绑定 */
    134. for (uint32_t i = 0; i < HDF_WIFI_FEATURE_NUM; i++) {
    135. if ((module->feList->fe[i] != NULL) && (chipConfig->featureMap & (1 << i))) {
    136. HDF_LOGI("%s:reg chip to feature %d", __func__, i);
    137. module->feList->fe[i]->chip = chipDriver;
    138. }
    139. }
    140. HDF_LOGI("%s:init chip %s success!", __func__, chipConfig->chipName);
    141. return HDF_SUCCESS;
    142. }
    143. /* WLAN平台初始化 */
    144. static int16_t HdfWifiPlatformInit(const struct HdfConfigWifiModuleConfig *config)
    145. {
    146. /* 根据配置进行module初始化 */
    147. int32_t ret = HdfWifiModuleInit(config);
    148. return ret;
    149. }
    150. return ret;
    151. }
    152. struct HdfWifiProductData *HdfWifiGetProduct(void)
    153. {
    154. return &g_hdfWifiProductData;
    155. }
    156. static int32_t HdfWifiDriverInit(struct HdfDeviceObject *device)
    157. {
    158. int32_t ret;
    159. HDF_LOGD("%s:start..", __func__);
    160. if (device == NULL) {
    161. return HDF_FAILURE;
    162. }
    163. /* 配置分析(初始化WLAN配置) */
    164. ret = HdfWifiProductInit(device);
    165. if (ret != HDF_SUCCESS) {
    166. HDF_LOGE("%s:init produt cfg error ret=%d", __func__, ret);
    167. return ret;
    168. }
    169. /* 获取全量配置的结构体对象 */
    170. struct HdfConfigWifiRoot *rootConfig = HdfWifiGetModuleConfigRoot();
    171. struct HdfWifiProductData *wifiData = HdfWifiGetProduct();
    172. const struct HdfConfigWifiModuleConfig *moduleConfig = &rootConfig->wifiConfig.moduleConfig;
    173. /* WIFI平台的初始化(Module初始化) */
    174. ret = HdfWifiPlatformInit(moduleConfig);
    175. if (ret) {
    176. HDF_LOGE("%s:init platform error ret=%d", __func__, ret);
    177. return ret;
    178. }
    179. /* 循环遍历所配置的芯片,并初始化有相应硬件的芯片 */
    180. for (int16_t i = 0; i < rootConfig->wifiConfig.deviceList.chipSize; i++) {
    181. const struct HdfConfigWifiChip *chipConfig = &rootConfig->wifiConfig.deviceList.chip[i];
    182. ret = HdfWifiChipInit(wifiData->module, chipConfig);
    183. if (ret != HDF_SUCCESS) {
    184. HDF_LOGE("%s:init chip %d error ret=%d, feature %d is unavailable!", __func__, i, ret,
    185. chipConfig->featureMap);
    186. } else {
    187. wifiData->device = device;
    188. HDF_LOGE("%s:init chip %d success!", __func__, i);
    189. break;
    190. }
    191. }
    192. HDF_LOGD("%s:success..", __func__);
    193. return ret;
    194. }
    195. int32_t HdfWifiDriverDispatch(struct HdfDeviceIoClient *client, int id, struct HdfSBuf *data, struct HdfSBuf *reply)
    196. {
    197. if (client == NULL) {
    198. return HDF_ERR_INVALID_PARAM;
    199. }
    200. return WifiCmdProcess(client->device, id, data, reply);
    201. }
    202. static int HdfWifiDriverBind(struct HdfDeviceObject *dev)
    203. {
    204. if (dev == NULL) {
    205. return HDF_FAILURE;
    206. }
    207. static struct IDeviceIoService wifiService = {
    208. .object.objectId = 1,
    209. .Dispatch = HdfWifiDriverDispatch,
    210. };
    211. dev->service = &wifiService;
    212. return HDF_SUCCESS;
    213. }
    214. static void HdfWifiDriverRelease(struct HdfDeviceObject *object)
    215. {
    216. (void)object;
    217. }
    218. struct HdfDriverEntry g_hdfWifiEntry = {
    219. .moduleVersion = 1,
    220. .Bind = HdfWifiDriverBind,
    221. .Init = HdfWifiDriverInit,
    222. .Release = HdfWifiDriverRelease,
    223. .moduleName = "HDF_WIFI"
    224. };