1 /* 2 * Copyright (c) 2012-2020 The Linux Foundation. All rights reserved. 3 * 4 * Permission to use, copy, modify, and/or distribute this software for 5 * any purpose with or without fee is hereby granted, provided that the 6 * above copyright notice and this permission notice appear in all 7 * copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /** 20 * DOC: wifi_pos_main.c 21 * This file defines the important functions pertinent to 22 * wifi positioning to initialize and de-initialize the component. 23 */ 24 #include "target_if_wifi_pos.h" 25 #include "wifi_pos_oem_interface_i.h" 26 #include "wifi_pos_utils_i.h" 27 #include "wifi_pos_api.h" 28 #include "wifi_pos_main_i.h" 29 #include "wifi_pos_ucfg_i.h" 30 #include "wlan_objmgr_cmn.h" 31 #include "wlan_objmgr_global_obj.h" 32 #include "wlan_objmgr_psoc_obj.h" 33 #include "wlan_objmgr_pdev_obj.h" 34 #include "wlan_objmgr_vdev_obj.h" 35 #include "wlan_ptt_sock_svc.h" 36 37 #include "wlan_reg_services_api.h" 38 /* forward declartion */ 39 struct regulatory_channel; 40 41 #define REG_SET_CHANNEL_REG_POWER(reg_info_1, val) do { \ 42 reg_info_1 &= 0xff00ffff; \ 43 reg_info_1 |= ((val & 0xff) << 16); \ 44 } while (0) 45 46 /* max tx power is in 1 dBm units */ 47 #define REG_SET_CHANNEL_MAX_TX_POWER(reg_info_2, val) do { \ 48 reg_info_2 &= 0xffff00ff; \ 49 reg_info_2 |= ((val & 0xff) << 8); \ 50 } while (0) 51 52 /* channel info consists of 6 bits of channel mode */ 53 54 #define REG_SET_CHANNEL_MODE(reg_channel, val) do { \ 55 (reg_channel)->info &= 0xffffffc0; \ 56 (reg_channel)->info |= (val); \ 57 } while (0) 58 59 /* 60 * obj mgr api to iterate over vdevs does not provide a direct array or vdevs, 61 * rather takes a callback that is called for every vdev. wifi pos needs to 62 * store device mode and vdev id of all active vdevs and provide this info to 63 * user space as part of APP registration response. due to this, vdev_idx is 64 * used to identify how many vdevs have been populated by obj manager API. 65 */ 66 static uint32_t vdev_idx; 67 68 /** 69 * wifi_pos_get_tlv_support: indicates if firmware supports TLV wifi pos msg 70 * @psoc: psoc object 71 * 72 * Return: status of operation 73 */ 74 static bool wifi_pos_get_tlv_support(struct wlan_objmgr_psoc *psoc) 75 { 76 /* this is TBD */ 77 return true; 78 } 79 80 struct wlan_lmac_if_wifi_pos_tx_ops * 81 wifi_pos_get_tx_ops(struct wlan_objmgr_psoc *psoc) 82 { 83 struct wlan_lmac_if_tx_ops *tx_ops; 84 85 if (!psoc) { 86 wifi_pos_err("psoc is null"); 87 return NULL; 88 } 89 90 tx_ops = wlan_psoc_get_lmac_if_txops(psoc); 91 if (!tx_ops) { 92 wifi_pos_err("tx_ops is NULL"); 93 return NULL; 94 } 95 96 return &tx_ops->wifi_pos_tx_ops; 97 } 98 99 #ifdef CNSS_GENL 100 static uint8_t * 101 wifi_pos_prepare_reg_resp(uint32_t *rsp_len, 102 struct app_reg_rsp_vdev_info *vdevs_info) 103 { 104 uint32_t *nl_sign; 105 uint8_t *resp_buf; 106 struct wifi_app_reg_rsp *app_reg_rsp; 107 108 /* 109 * allocate ENHNC_FLAGS_LEN i.e. 4bytes extra memory in app_reg_resp 110 * to indicate NLA type response is supported for OEM request 111 * commands. 112 */ 113 *rsp_len = (sizeof(struct app_reg_rsp_vdev_info) * vdev_idx) 114 + sizeof(uint8_t) + ENHNC_FLAGS_LEN; 115 resp_buf = qdf_mem_malloc(*rsp_len); 116 if (!resp_buf) 117 return NULL; 118 119 app_reg_rsp = (struct wifi_app_reg_rsp *)resp_buf; 120 app_reg_rsp->num_inf = vdev_idx; 121 qdf_mem_copy(&app_reg_rsp->vdevs, vdevs_info, 122 sizeof(struct app_reg_rsp_vdev_info) * vdev_idx); 123 124 nl_sign = (uint32_t *)&app_reg_rsp->vdevs[vdev_idx]; 125 *nl_sign |= NL_ENABLE_OEM_REQ_RSP; 126 127 return resp_buf; 128 } 129 130 /** 131 * wifi_pos_get_host_pdev_id: Get host pdev_id 132 * @psoc: Pointer to psoc object 133 * @tgt_pdev_id: target_pdev_id 134 * @host_pdev_id: host pdev_id 135 * 136 * Return: QDF_STATUS_SUCCESS in case of success, error codes in case of failure 137 */ 138 static QDF_STATUS wifi_pos_get_host_pdev_id( 139 struct wlan_objmgr_psoc *psoc, uint32_t tgt_pdev_id, 140 uint32_t *host_pdev_id) 141 { 142 /* pdev_id in FW starts from 1. So convert it to 143 * host id by decrementing it. 144 * zero has special meaning due to backward 145 * compatibility. Dont change it. 146 */ 147 if (tgt_pdev_id) 148 *host_pdev_id = tgt_pdev_id - 1; 149 else 150 *host_pdev_id = tgt_pdev_id; 151 152 return QDF_STATUS_SUCCESS; 153 } 154 #else 155 static uint8_t * 156 wifi_pos_prepare_reg_resp(uint32_t *rsp_len, 157 struct app_reg_rsp_vdev_info *vdevs_info) 158 { 159 uint8_t *resp_buf; 160 struct wifi_app_reg_rsp *app_reg_rsp; 161 162 *rsp_len = (sizeof(struct app_reg_rsp_vdev_info) * vdev_idx) 163 + sizeof(uint8_t); 164 resp_buf = qdf_mem_malloc(*rsp_len); 165 if (!resp_buf) 166 return NULL; 167 168 app_reg_rsp = (struct wifi_app_reg_rsp *)resp_buf; 169 app_reg_rsp->num_inf = vdev_idx; 170 qdf_mem_copy(&app_reg_rsp->vdevs, vdevs_info, 171 sizeof(struct app_reg_rsp_vdev_info) * vdev_idx); 172 173 return resp_buf; 174 } 175 176 static QDF_STATUS wifi_pos_get_host_pdev_id( 177 struct wlan_objmgr_psoc *psoc, uint32_t tgt_pdev_id, 178 uint32_t *host_pdev_id) 179 { 180 struct wlan_lmac_if_wifi_pos_tx_ops *tx_ops; 181 182 tx_ops = wifi_pos_get_tx_ops(psoc); 183 if (!tx_ops) { 184 qdf_print("tx ops null"); 185 return QDF_STATUS_E_NULL_VALUE; 186 } 187 188 if (!tx_ops->wifi_pos_convert_pdev_id_target_to_host) { 189 wifi_pos_err("wifi_pos_convert_pdev_id_target_to_host is null"); 190 return QDF_STATUS_E_NULL_VALUE; 191 } 192 193 return tx_ops->wifi_pos_convert_pdev_id_target_to_host( 194 psoc, tgt_pdev_id, host_pdev_id); 195 } 196 #endif 197 198 static QDF_STATUS wifi_pos_process_data_req(struct wlan_objmgr_psoc *psoc, 199 struct wifi_pos_req_msg *req) 200 { 201 uint8_t idx; 202 uint32_t sub_type = 0; 203 uint32_t channel_mhz = 0; 204 uint32_t host_pdev_id = 0, tgt_pdev_id = 0; 205 uint32_t offset; 206 struct oem_data_req data_req; 207 struct wlan_lmac_if_wifi_pos_tx_ops *tx_ops; 208 struct wlan_objmgr_pdev *pdev; 209 struct wifi_pos_psoc_priv_obj *wifi_pos_obj = 210 wifi_pos_get_psoc_priv_obj(wifi_pos_get_psoc()); 211 QDF_STATUS status; 212 213 214 if (!wifi_pos_obj) { 215 wifi_pos_err("wifi_pos priv obj is null"); 216 return QDF_STATUS_E_INVAL; 217 } 218 219 wifi_pos_debug("Received data req pid(%d), len(%d)", 220 req->pid, req->buf_len); 221 222 /* look for fields */ 223 if (req->field_info_buf) 224 for (idx = 0; idx < req->field_info_buf->count; idx++) { 225 offset = req->field_info_buf->fields[idx].offset; 226 /* 227 * replace following reads with read_api based on 228 * length 229 */ 230 if (req->field_info_buf->fields[idx].id == 231 META_DATA_SUB_TYPE) { 232 sub_type = *((uint32_t *)&req->buf[offset]); 233 continue; 234 } 235 236 if (req->field_info_buf->fields[idx].id == 237 META_DATA_CHANNEL_MHZ) { 238 channel_mhz = *((uint32_t *)&req->buf[offset]); 239 continue; 240 } 241 242 if (req->field_info_buf->fields[idx].id == 243 META_DATA_PDEV) { 244 tgt_pdev_id = *((uint32_t *)&req->buf[offset]); 245 status = wifi_pos_get_host_pdev_id( 246 psoc, tgt_pdev_id, 247 &host_pdev_id); 248 if (QDF_IS_STATUS_ERROR(status)) { 249 wifi_pos_err("failed to get host pdev_id, tgt_pdev_id = %d", 250 tgt_pdev_id); 251 return QDF_STATUS_E_INVAL; 252 } 253 continue; 254 } 255 } 256 257 switch (sub_type) { 258 case TARGET_OEM_CAPABILITY_REQ: 259 /* TBD */ 260 break; 261 case TARGET_OEM_CONFIGURE_LCR: 262 /* TBD */ 263 break; 264 case TARGET_OEM_CONFIGURE_LCI: 265 /* TBD */ 266 break; 267 case TARGET_OEM_MEASUREMENT_REQ: 268 /* TBD */ 269 break; 270 case TARGET_OEM_CONFIGURE_FTMRR: 271 wifi_pos_debug("FTMRR request"); 272 if (wifi_pos_obj->wifi_pos_send_action) 273 wifi_pos_obj->wifi_pos_send_action(psoc, sub_type, 274 req->buf, 275 req->buf_len); 276 break; 277 case TARGET_OEM_CONFIGURE_WRU: 278 wifi_pos_debug("WRU request"); 279 if (wifi_pos_obj->wifi_pos_send_action) 280 wifi_pos_obj->wifi_pos_send_action(psoc, sub_type, 281 req->buf, 282 req->buf_len); 283 break; 284 default: 285 wifi_pos_debug("invalid sub type or not passed"); 286 287 tx_ops = wifi_pos_get_tx_ops(psoc); 288 if (!tx_ops) { 289 wifi_pos_err("tx ops null"); 290 return QDF_STATUS_E_INVAL; 291 } 292 293 pdev = wlan_objmgr_get_pdev_by_id(psoc, host_pdev_id, 294 WLAN_WIFI_POS_CORE_ID); 295 if (!pdev) { 296 wifi_pos_err("pdev null"); 297 return QDF_STATUS_E_INVAL; 298 } 299 data_req.data_len = req->buf_len; 300 data_req.data = req->buf; 301 tx_ops->data_req_tx(pdev, &data_req); 302 wlan_objmgr_pdev_release_ref(pdev, 303 WLAN_WIFI_POS_CORE_ID); 304 break; 305 } 306 307 return QDF_STATUS_SUCCESS; 308 } 309 310 static QDF_STATUS wifi_pos_process_set_cap_req(struct wlan_objmgr_psoc *psoc, 311 struct wifi_pos_req_msg *req) 312 { 313 int error_code; 314 struct wifi_pos_psoc_priv_obj *wifi_pos_obj = 315 wifi_pos_get_psoc_priv_obj(psoc); 316 struct wifi_pos_user_defined_caps *caps = 317 (struct wifi_pos_user_defined_caps *)req->buf; 318 319 if (!wifi_pos_obj) { 320 wifi_pos_err("wifi_pos priv obj is null"); 321 return QDF_STATUS_E_INVAL; 322 } 323 324 wifi_pos_debug("Received set cap req pid(%d), len(%d)", 325 req->pid, req->buf_len); 326 327 wifi_pos_obj->ftm_rr = caps->ftm_rr; 328 wifi_pos_obj->lci_capability = caps->lci_capability; 329 error_code = qdf_status_to_os_return(QDF_STATUS_SUCCESS); 330 wifi_pos_obj->wifi_pos_send_rsp(psoc, wifi_pos_obj->app_pid, 331 WIFI_POS_CMD_SET_CAPS, 332 sizeof(error_code), 333 (uint8_t *)&error_code); 334 335 return QDF_STATUS_SUCCESS; 336 } 337 338 static QDF_STATUS wifi_pos_process_get_cap_req(struct wlan_objmgr_psoc *psoc, 339 struct wifi_pos_req_msg *req) 340 { 341 struct wifi_pos_oem_get_cap_rsp cap_rsp = { { {0} } }; 342 struct wifi_pos_psoc_priv_obj *wifi_pos_obj = 343 wifi_pos_get_psoc_priv_obj(psoc); 344 345 if (!wifi_pos_obj) { 346 wifi_pos_err("wifi_pos priv obj is null"); 347 return QDF_STATUS_E_INVAL; 348 } 349 350 wifi_pos_debug("Received get cap req pid(%d), len(%d)", 351 req->pid, req->buf_len); 352 353 wifi_pos_populate_caps(psoc, &cap_rsp.driver_cap); 354 cap_rsp.user_defined_cap.ftm_rr = wifi_pos_obj->ftm_rr; 355 cap_rsp.user_defined_cap.lci_capability = wifi_pos_obj->lci_capability; 356 357 wifi_pos_obj->wifi_pos_send_rsp(psoc, wifi_pos_obj->app_pid, 358 WIFI_POS_CMD_GET_CAPS, 359 sizeof(cap_rsp), 360 (uint8_t *)&cap_rsp); 361 362 return QDF_STATUS_SUCCESS; 363 } 364 365 QDF_STATUS wifi_pos_send_report_resp(struct wlan_objmgr_psoc *psoc, 366 int req_id, uint8_t *dest_mac, 367 int err_code) 368 { 369 struct wifi_pos_err_msg_report err_report = {0}; 370 struct wifi_pos_psoc_priv_obj *wifi_pos_obj = 371 wifi_pos_get_psoc_priv_obj(psoc); 372 373 if (!wifi_pos_obj) { 374 wifi_pos_err("wifi_pos priv obj is null"); 375 return QDF_STATUS_E_INVAL; 376 } 377 378 err_report.msg_tag_len = OEM_MSG_RSP_HEAD_TAG_ID << 16; 379 err_report.msg_tag_len |= (sizeof(err_report) - 380 sizeof(err_report.err_rpt)) & 0x0000FFFF; 381 err_report.msg_subtype = TARGET_OEM_ERROR_REPORT_RSP; 382 err_report.req_id = req_id & 0xFFFF; 383 err_report.req_id |= ((err_code & 0xFF) << 16); 384 err_report.req_id |= (0x1 << 24); 385 err_report.time_left = 0xFFFFFFFF; 386 err_report.err_rpt.tag_len = OEM_MEAS_RSP_HEAD_TAG_ID << 16; 387 err_report.err_rpt.tag_len |= 388 (sizeof(struct wifi_pos_err_rpt)) & 0x0000FFFF; 389 memcpy(&err_report.err_rpt.dest_mac, dest_mac, QDF_MAC_ADDR_SIZE); 390 391 wifi_pos_obj->wifi_pos_send_rsp(psoc, wifi_pos_obj->app_pid, 392 WIFI_POS_CMD_OEM_DATA, 393 sizeof(err_report), 394 (uint8_t *)&err_report); 395 396 return QDF_STATUS_SUCCESS; 397 } 398 399 static void wifi_update_channel_bw_info(struct wlan_objmgr_psoc *psoc, 400 struct wlan_objmgr_pdev *pdev, 401 uint16_t freq, 402 struct wifi_pos_ch_info_rsp *chan_info) 403 { 404 struct ch_params ch_params = {0}; 405 uint16_t sec_ch_2g = 0; 406 struct wifi_pos_psoc_priv_obj *wifi_pos_psoc = 407 wifi_pos_get_psoc_priv_obj(psoc); 408 uint32_t phy_mode; 409 410 if (!wifi_pos_psoc) { 411 wifi_pos_err("wifi_pos priv obj is null"); 412 return; 413 } 414 415 /* Passing CH_WIDTH_MAX will give the max bandwidth supported */ 416 ch_params.ch_width = CH_WIDTH_MAX; 417 wlan_reg_set_channel_params_for_freq(pdev, freq, 418 sec_ch_2g, &ch_params); 419 chan_info->band_center_freq1 = ch_params.mhz_freq_seg0; 420 wifi_pos_psoc->wifi_pos_get_fw_phy_mode_for_freq(freq, 421 ch_params.ch_width, 422 &phy_mode); 423 REG_SET_CHANNEL_MODE(chan_info, phy_mode); 424 } 425 426 static void wifi_pos_get_reg_info(struct wlan_objmgr_pdev *pdev, 427 uint16_t freq, uint32_t *reg_info_1, 428 uint32_t *reg_info_2) 429 { 430 uint32_t reg_power = wlan_reg_get_channel_reg_power_for_freq(pdev, 431 freq); 432 433 *reg_info_1 = 0; 434 *reg_info_2 = 0; 435 436 REG_SET_CHANNEL_REG_POWER(*reg_info_1, reg_power); 437 REG_SET_CHANNEL_MAX_TX_POWER(*reg_info_2, reg_power); 438 } 439 440 /** 441 * wifi_pos_get_valid_channels: Get the list of valid channels from the 442 * given channel list 443 * @chan_freqs: Channel frequencies to be validated 444 * @num_ch: NUmber of channels in the channel list to be validated 445 * @valid_channel_list: Pointer to valid channel list 446 * 447 * Return: Number of valid channels in the given list 448 */ 449 static uint32_t wifi_pos_get_valid_channels(qdf_freq_t *chan_freqs, 450 uint32_t num_ch, 451 qdf_freq_t *valid_channel_list) 452 { 453 uint32_t i, num_valid_channels = 0; 454 455 for (i = 0; i < num_ch; i++) { 456 if (wlan_reg_get_chan_enum_for_freq(chan_freqs[i]) == 457 INVALID_CHANNEL) 458 continue; 459 valid_channel_list[num_valid_channels++] = chan_freqs[i]; 460 } 461 return num_valid_channels; 462 } 463 464 static void wifi_pos_pdev_iterator(struct wlan_objmgr_psoc *psoc, 465 void *obj, void *arg) 466 { 467 QDF_STATUS status; 468 uint8_t num_channels; 469 struct wlan_objmgr_pdev *pdev = obj; 470 struct wifi_pos_channel_list *chan_list = arg; 471 struct channel_power *ch_info = NULL; 472 473 if (!chan_list) { 474 wifi_pos_err("wifi_pos priv arg is null"); 475 return; 476 } 477 ch_info = (struct channel_power *)chan_list->chan_info; 478 status = wlan_reg_get_channel_list_with_power(pdev, ch_info, 479 &num_channels); 480 481 if (QDF_IS_STATUS_ERROR(status)) { 482 wifi_pos_err("Failed to get valid channel list"); 483 return; 484 } 485 chan_list->num_channels = num_channels; 486 } 487 488 static void wifi_pos_get_ch_info(struct wlan_objmgr_psoc *psoc, 489 struct wifi_pos_channel_list *chan_list) 490 { 491 wlan_objmgr_iterate_obj_list(psoc, WLAN_PDEV_OP, 492 wifi_pos_pdev_iterator, 493 chan_list, true, WLAN_WIFI_POS_CORE_ID); 494 wifi_pos_notice("num channels: %d", chan_list->num_channels); 495 } 496 497 static QDF_STATUS wifi_pos_process_ch_info_req(struct wlan_objmgr_psoc *psoc, 498 struct wifi_pos_req_msg *req) 499 { 500 uint8_t idx; 501 uint8_t *buf = NULL; 502 uint32_t len, i, freq; 503 uint32_t reg_info_1; 504 uint32_t reg_info_2; 505 qdf_freq_t *chan_freqs = NULL; 506 bool oem_6g_support_disable; 507 uint8_t *channels = req->buf; 508 struct wlan_objmgr_pdev *pdev; 509 uint32_t num_ch = req->buf_len; 510 qdf_freq_t valid_channel_list[NUM_CHANNELS]; 511 uint32_t num_valid_channels = 0; 512 struct wifi_pos_ch_info_rsp *ch_info; 513 struct wifi_pos_channel_list *ch_list = NULL; 514 struct wifi_pos_psoc_priv_obj *wifi_pos_obj = 515 wifi_pos_get_psoc_priv_obj(psoc); 516 QDF_STATUS ret_val; 517 518 if (!wifi_pos_obj) { 519 wifi_pos_err("wifi_pos priv obj is null"); 520 return QDF_STATUS_E_INVAL; 521 } 522 523 wifi_pos_debug("Received ch info req pid(%d), len(%d)", 524 req->pid, req->buf_len); 525 526 /* get first pdev since we need that only for freq and dfs state */ 527 pdev = wlan_objmgr_get_pdev_by_id(psoc, 0, WLAN_WIFI_POS_CORE_ID); 528 if (!pdev) { 529 wifi_pos_err("pdev get API failed"); 530 return QDF_STATUS_E_INVAL; 531 } 532 if (num_ch > NUM_CHANNELS) { 533 wifi_pos_err("Invalid number of channels"); 534 ret_val = QDF_STATUS_E_INVAL; 535 goto cleanup; 536 } 537 538 chan_freqs = qdf_mem_malloc(NUM_CHANNELS * (sizeof(*chan_freqs))); 539 if (!chan_freqs) { 540 ret_val = QDF_STATUS_E_NOMEM; 541 goto cleanup; 542 } 543 544 ch_list = qdf_mem_malloc(sizeof(*ch_list)); 545 if (!ch_list) { 546 ret_val = QDF_STATUS_E_NOMEM; 547 goto cleanup; 548 } 549 550 if (num_ch == 0 && req->rsp_version == WIFI_POS_RSP_V2_NL) { 551 wifi_pos_get_ch_info(psoc, ch_list); 552 qdf_spin_lock_bh(&wifi_pos_obj->wifi_pos_lock); 553 oem_6g_support_disable = wifi_pos_obj->oem_6g_support_disable; 554 qdf_spin_unlock_bh(&wifi_pos_obj->wifi_pos_lock); 555 556 /* ch_list has the frequencies in order of 2.4g, 5g & 6g */ 557 for (i = 0; i < ch_list->num_channels; i++) { 558 freq = ch_list->chan_info[i].center_freq; 559 if (oem_6g_support_disable && 560 WLAN_REG_IS_6GHZ_CHAN_FREQ(freq)) 561 continue; 562 num_valid_channels++; 563 } 564 } else { 565 for (i = 0; i < NUM_CHANNELS; i++) 566 chan_freqs[i] = 567 wlan_reg_chan_band_to_freq(pdev, channels[i], 568 BIT(REG_BAND_5G) | 569 BIT(REG_BAND_2G)); 570 /* v1 has ch_list with frequencies in order of 2.4g, 5g only */ 571 num_valid_channels = wifi_pos_get_valid_channels( 572 chan_freqs, num_ch, 573 valid_channel_list); 574 for (i = 0; i < num_valid_channels; i++) { 575 ch_list->chan_info[i].center_freq = 576 valid_channel_list[i]; 577 ch_list->chan_info[i].chan_num = 578 wlan_reg_freq_to_chan(pdev, ch_list-> 579 chan_info[i].center_freq); 580 } 581 } 582 583 len = sizeof(uint8_t) + sizeof(struct wifi_pos_ch_info_rsp) * 584 num_valid_channels; 585 buf = qdf_mem_malloc(len); 586 if (!buf) { 587 ret_val = QDF_STATUS_E_NOMEM; 588 goto cleanup; 589 } 590 591 /* First byte of message body will have num of channels */ 592 buf[0] = num_valid_channels; 593 ch_info = (struct wifi_pos_ch_info_rsp *)&buf[1]; 594 for (idx = 0; idx < num_valid_channels; idx++) { 595 ch_info[idx].reserved0 = 0; 596 ch_info[idx].chan_id = ch_list->chan_info[idx].chan_num; 597 ch_info[idx].mhz = ch_list->chan_info[idx].center_freq; 598 ch_info[idx].band_center_freq1 = ch_info[idx].mhz; 599 ch_info[idx].band_center_freq2 = 0; 600 ch_info[idx].info = 0; 601 wifi_pos_get_reg_info(pdev, ch_info[idx].mhz, 602 ®_info_1, ®_info_2); 603 604 if (wlan_reg_is_dfs_for_freq(pdev, ch_info[idx].mhz)) 605 WIFI_POS_SET_DFS(ch_info[idx].info); 606 607 wifi_update_channel_bw_info(psoc, pdev, 608 ch_info[idx].mhz, 609 &ch_info[idx]); 610 611 ch_info[idx].reg_info_1 = reg_info_1; 612 ch_info[idx].reg_info_2 = reg_info_2; 613 } 614 615 wifi_pos_obj->wifi_pos_send_rsp(psoc, wifi_pos_obj->app_pid, 616 WIFI_POS_CMD_GET_CH_INFO, 617 len, buf); 618 ret_val = QDF_STATUS_SUCCESS; 619 620 cleanup: 621 qdf_mem_free(buf); 622 qdf_mem_free(ch_list); 623 qdf_mem_free(chan_freqs); 624 wlan_objmgr_pdev_release_ref(pdev, WLAN_WIFI_POS_CORE_ID); 625 626 return ret_val; 627 } 628 629 static void wifi_pos_vdev_iterator(struct wlan_objmgr_psoc *psoc, 630 void *vdev, void *arg) 631 { 632 struct app_reg_rsp_vdev_info *vdev_info = arg; 633 634 vdev_info[vdev_idx].dev_mode = wlan_vdev_mlme_get_opmode(vdev); 635 vdev_info[vdev_idx].vdev_id = wlan_vdev_get_id(vdev); 636 vdev_idx++; 637 } 638 639 static QDF_STATUS wifi_pos_process_app_reg_req(struct wlan_objmgr_psoc *psoc, 640 struct wifi_pos_req_msg *req) 641 { 642 QDF_STATUS ret = QDF_STATUS_SUCCESS; 643 uint8_t err = 0, *app_reg_rsp; 644 uint32_t rsp_len; 645 char *sign_str = NULL; 646 struct app_reg_rsp_vdev_info vdevs_info[WLAN_UMAC_PSOC_MAX_VDEVS] 647 = { { 0 } }; 648 struct wifi_pos_psoc_priv_obj *wifi_pos_obj = 649 wifi_pos_get_psoc_priv_obj(psoc); 650 651 if (!wifi_pos_obj) { 652 wifi_pos_err("wifi_pos priv obj is null"); 653 return QDF_STATUS_E_INVAL; 654 } 655 656 wifi_pos_err("Received App Req Req pid(%d), len(%d)", 657 req->pid, req->buf_len); 658 659 sign_str = (char *)req->buf; 660 /* Registration request is only allowed for QTI Application */ 661 if ((OEM_APP_SIGNATURE_LEN != req->buf_len) || 662 (strncmp(sign_str, OEM_APP_SIGNATURE_STR, 663 OEM_APP_SIGNATURE_LEN))) { 664 wifi_pos_err("Invalid signature pid(%d)", req->pid); 665 ret = QDF_STATUS_E_PERM; 666 err = OEM_ERR_INVALID_SIGNATURE; 667 goto app_reg_failed; 668 } 669 670 wifi_pos_debug("Valid App Req Req from pid(%d)", req->pid); 671 qdf_spin_lock_bh(&wifi_pos_obj->wifi_pos_lock); 672 wifi_pos_obj->is_app_registered = true; 673 wifi_pos_obj->app_pid = req->pid; 674 qdf_spin_unlock_bh(&wifi_pos_obj->wifi_pos_lock); 675 676 vdev_idx = 0; 677 wlan_objmgr_iterate_obj_list(psoc, WLAN_VDEV_OP, 678 wifi_pos_vdev_iterator, 679 vdevs_info, true, WLAN_WIFI_POS_CORE_ID); 680 681 app_reg_rsp = wifi_pos_prepare_reg_resp(&rsp_len, vdevs_info); 682 if (!app_reg_rsp) { 683 ret = QDF_STATUS_E_NOMEM; 684 err = OEM_ERR_NULL_CONTEXT; 685 goto app_reg_failed; 686 } 687 688 if (!vdev_idx) 689 wifi_pos_debug("no active vdev"); 690 691 vdev_idx = 0; 692 wifi_pos_obj->wifi_pos_send_rsp(psoc, req->pid, 693 WIFI_POS_CMD_REGISTRATION, 694 rsp_len, (uint8_t *)app_reg_rsp); 695 696 qdf_mem_free(app_reg_rsp); 697 return ret; 698 699 app_reg_failed: 700 701 wifi_pos_obj->wifi_pos_send_rsp(psoc, req->pid, WIFI_POS_CMD_ERROR, 702 sizeof(err), &err); 703 return ret; 704 } 705 706 /** 707 * wifi_pos_tlv_callback: wifi pos msg handler registered for TLV type req 708 * @wmi_msg: wmi type request msg 709 * 710 * Return: status of operation 711 */ 712 static QDF_STATUS wifi_pos_tlv_callback(struct wlan_objmgr_psoc *psoc, 713 struct wifi_pos_req_msg *req) 714 { 715 wifi_pos_debug("enter: msg_type: %d", req->msg_type); 716 switch (req->msg_type) { 717 case WIFI_POS_CMD_REGISTRATION: 718 return wifi_pos_process_app_reg_req(psoc, req); 719 case WIFI_POS_CMD_OEM_DATA: 720 return wifi_pos_process_data_req(psoc, req); 721 case WIFI_POS_CMD_GET_CH_INFO: 722 return wifi_pos_process_ch_info_req(psoc, req); 723 case WIFI_POS_CMD_SET_CAPS: 724 return wifi_pos_process_set_cap_req(psoc, req); 725 case WIFI_POS_CMD_GET_CAPS: 726 return wifi_pos_process_get_cap_req(psoc, req); 727 default: 728 wifi_pos_err("invalid request type"); 729 break; 730 } 731 return 0; 732 } 733 734 /** 735 * wifi_pos_non_tlv_callback: wifi pos msg handler registered for non-TLV 736 * type req 737 * @wmi_msg: wmi type request msg 738 * 739 * Return: status of operation 740 */ 741 static QDF_STATUS wifi_pos_non_tlv_callback(struct wlan_objmgr_psoc *psoc, 742 struct wifi_pos_req_msg *req) 743 { 744 return QDF_STATUS_SUCCESS; 745 } 746 747 QDF_STATUS wifi_pos_convert_host_pdev_id_to_target( 748 struct wlan_objmgr_psoc *psoc, uint32_t host_pdev_id, 749 uint32_t *target_pdev_id) 750 { 751 struct wlan_lmac_if_wifi_pos_tx_ops *tx_ops; 752 753 tx_ops = wifi_pos_get_tx_ops(psoc); 754 if (!tx_ops) { 755 wifi_pos_err("tx_ops is null"); 756 return QDF_STATUS_E_NULL_VALUE; 757 } 758 759 if (!tx_ops->wifi_pos_convert_pdev_id_host_to_target) { 760 wifi_pos_err("wifi_pos_convert_pdev_id_host_to_target is null"); 761 return QDF_STATUS_E_NULL_VALUE; 762 } 763 764 return tx_ops->wifi_pos_convert_pdev_id_host_to_target( 765 psoc, host_pdev_id, target_pdev_id); 766 } 767 768 QDF_STATUS wifi_pos_psoc_obj_created_notification( 769 struct wlan_objmgr_psoc *psoc, void *arg_list) 770 { 771 QDF_STATUS status; 772 struct wifi_pos_psoc_priv_obj *wifi_pos_obj; 773 774 /* 775 * this is for WIN, if they have multiple psoc, we dont want to create 776 * multiple priv object. Since there is just one LOWI app registered to 777 * one driver, avoid 2nd private object with another psoc. 778 */ 779 if (wifi_pos_get_psoc()) { 780 wifi_pos_debug("global psoc obj already set. do not allocate another psoc private object"); 781 return QDF_STATUS_SUCCESS; 782 } else { 783 wifi_pos_debug("setting global pos object"); 784 wifi_pos_set_psoc(psoc); 785 } 786 787 /* initialize wifi-pos psoc priv object */ 788 wifi_pos_obj = qdf_mem_malloc(sizeof(*wifi_pos_obj)); 789 if (!wifi_pos_obj) { 790 wifi_pos_clear_psoc(); 791 return QDF_STATUS_E_NOMEM; 792 } 793 794 qdf_spinlock_create(&wifi_pos_obj->wifi_pos_lock); 795 /* Register TLV or non-TLV callbacks depending on target fw version */ 796 if (wifi_pos_get_tlv_support(psoc)) 797 wifi_pos_obj->wifi_pos_req_handler = wifi_pos_tlv_callback; 798 else 799 wifi_pos_obj->wifi_pos_req_handler = wifi_pos_non_tlv_callback; 800 801 /* 802 * MGMT Rx is not handled in this phase since wifi pos only uses few 803 * measurement subtypes under RRM_RADIO_MEASURE_REQ. Rest of them are 804 * used for 80211k. That part is not yet converged and still follows 805 * legacy MGMT Rx to work. Action frame in new TXRX can be registered 806 * at per ACTION Frame type granularity only. 807 */ 808 809 status = wlan_objmgr_psoc_component_obj_attach(psoc, 810 WLAN_UMAC_COMP_WIFI_POS, 811 wifi_pos_obj, 812 QDF_STATUS_SUCCESS); 813 814 if (QDF_IS_STATUS_ERROR(status)) { 815 wifi_pos_err("obj attach with psoc failed with status: %d", 816 status); 817 qdf_spinlock_destroy(&wifi_pos_obj->wifi_pos_lock); 818 qdf_mem_free(wifi_pos_obj); 819 wifi_pos_clear_psoc(); 820 } 821 822 return status; 823 } 824 825 QDF_STATUS wifi_pos_psoc_obj_destroyed_notification( 826 struct wlan_objmgr_psoc *psoc, void *arg_list) 827 { 828 QDF_STATUS status; 829 struct wifi_pos_psoc_priv_obj *wifi_pos_obj = NULL; 830 831 if (wifi_pos_get_psoc() == psoc) { 832 wifi_pos_debug("deregistering wifi_pos_psoc object"); 833 wifi_pos_clear_psoc(); 834 } else { 835 wifi_pos_warn("un-related PSOC closed. do nothing"); 836 return QDF_STATUS_SUCCESS; 837 } 838 839 wifi_pos_obj = wifi_pos_get_psoc_priv_obj(psoc); 840 if (!wifi_pos_obj) { 841 wifi_pos_err("wifi_pos_obj is NULL"); 842 return QDF_STATUS_E_FAULT; 843 } 844 845 target_if_wifi_pos_deinit_dma_rings(psoc); 846 847 status = wlan_objmgr_psoc_component_obj_detach(psoc, 848 WLAN_UMAC_COMP_WIFI_POS, 849 wifi_pos_obj); 850 if (status != QDF_STATUS_SUCCESS) 851 wifi_pos_err("wifi_pos_obj detach failed"); 852 853 wifi_pos_debug("wifi_pos_obj deleted with status %d", status); 854 qdf_spinlock_destroy(&wifi_pos_obj->wifi_pos_lock); 855 qdf_mem_free(wifi_pos_obj); 856 857 return status; 858 } 859 860 int wifi_pos_oem_rsp_handler(struct wlan_objmgr_psoc *psoc, 861 struct oem_data_rsp *oem_rsp) 862 { 863 uint32_t len; 864 uint8_t *data; 865 uint32_t app_pid; 866 struct wifi_pos_psoc_priv_obj *priv; 867 wifi_pos_send_rsp_handler wifi_pos_send_rsp; 868 869 priv = wifi_pos_get_psoc_priv_obj(wifi_pos_get_psoc()); 870 if (!priv) { 871 wifi_pos_err("private object is NULL"); 872 return -EINVAL; 873 } 874 875 qdf_spin_lock_bh(&priv->wifi_pos_lock); 876 app_pid = priv->app_pid; 877 wifi_pos_send_rsp = priv->wifi_pos_send_rsp; 878 qdf_spin_unlock_bh(&priv->wifi_pos_lock); 879 880 len = oem_rsp->rsp_len_1 + oem_rsp->rsp_len_2 + oem_rsp->dma_len; 881 if (oem_rsp->rsp_len_1 > OEM_DATA_RSP_SIZE || 882 oem_rsp->rsp_len_2 > OEM_DATA_RSP_SIZE) { 883 wifi_pos_err("invalid length of Oem Data response"); 884 return -EINVAL; 885 } 886 887 if (!wifi_pos_send_rsp) { 888 wifi_pos_err("invalid response handler"); 889 return -EINVAL; 890 } 891 892 wifi_pos_debug("oem data rsp, len: %d to pid: %d", len, app_pid); 893 894 if (oem_rsp->rsp_len_2 + oem_rsp->dma_len) { 895 /* stitch togther the msg data_1 + CIR/CFR + data_2 */ 896 data = qdf_mem_malloc(len); 897 if (!data) 898 return -ENOMEM; 899 900 qdf_mem_copy(data, oem_rsp->data_1, oem_rsp->rsp_len_1); 901 qdf_mem_copy(&data[oem_rsp->rsp_len_1], 902 oem_rsp->vaddr, oem_rsp->dma_len); 903 qdf_mem_copy(&data[oem_rsp->rsp_len_1 + oem_rsp->dma_len], 904 oem_rsp->data_2, oem_rsp->rsp_len_2); 905 906 wifi_pos_send_rsp(psoc, app_pid, WIFI_POS_CMD_OEM_DATA, len, 907 data); 908 qdf_mem_free(data); 909 } else { 910 wifi_pos_send_rsp(psoc, app_pid, WIFI_POS_CMD_OEM_DATA, 911 oem_rsp->rsp_len_1, oem_rsp->data_1); 912 } 913 914 return 0; 915 } 916 917 void wifi_pos_register_rx_ops(struct wlan_lmac_if_rx_ops *rx_ops) 918 { 919 struct wlan_lmac_if_wifi_pos_rx_ops *wifi_pos_rx_ops; 920 921 wifi_pos_rx_ops = &rx_ops->wifi_pos_rx_ops; 922 wifi_pos_rx_ops->oem_rsp_event_rx = wifi_pos_oem_rsp_handler; 923 } 924 925 QDF_STATUS wifi_pos_populate_caps(struct wlan_objmgr_psoc *psoc, 926 struct wifi_pos_driver_caps *caps) 927 { 928 uint16_t i, count = 0; 929 uint32_t freq; 930 struct wifi_pos_psoc_priv_obj *wifi_pos_obj = 931 wifi_pos_get_psoc_priv_obj(psoc); 932 struct wifi_pos_channel_list *ch_list = NULL; 933 934 wifi_pos_debug("Enter"); 935 if (!wifi_pos_obj) { 936 wifi_pos_err("wifi_pos_obj is null"); 937 return QDF_STATUS_E_NULL_VALUE; 938 } 939 940 ch_list = qdf_mem_malloc(sizeof(*ch_list)); 941 if (!ch_list) 942 return QDF_STATUS_E_NOMEM; 943 944 strlcpy(caps->oem_target_signature, 945 OEM_TARGET_SIGNATURE, 946 OEM_TARGET_SIGNATURE_LEN); 947 caps->oem_target_type = wifi_pos_obj->oem_target_type; 948 caps->oem_fw_version = wifi_pos_obj->oem_fw_version; 949 caps->driver_version.major = wifi_pos_obj->driver_version.major; 950 caps->driver_version.minor = wifi_pos_obj->driver_version.minor; 951 caps->driver_version.patch = wifi_pos_obj->driver_version.patch; 952 caps->driver_version.build = wifi_pos_obj->driver_version.build; 953 caps->allowed_dwell_time_min = wifi_pos_obj->allowed_dwell_time_min; 954 caps->allowed_dwell_time_max = wifi_pos_obj->allowed_dwell_time_max; 955 caps->curr_dwell_time_min = wifi_pos_obj->current_dwell_time_min; 956 caps->curr_dwell_time_max = wifi_pos_obj->current_dwell_time_max; 957 caps->supported_bands = wlan_objmgr_psoc_get_band_capability(psoc); 958 wifi_pos_get_ch_info(psoc, ch_list); 959 960 /* copy valid channels list to caps */ 961 for (i = 0; i < ch_list->num_channels; i++) { 962 freq = ch_list->chan_info[i].center_freq; 963 if (WLAN_REG_IS_6GHZ_CHAN_FREQ(freq)) 964 continue; 965 caps->channel_list[count++] = ch_list->chan_info[i].chan_num; 966 } 967 caps->num_channels = count; 968 qdf_mem_free(ch_list); 969 return QDF_STATUS_SUCCESS; 970 } 971