/* * Copyright (c) 202X Amlogic, Inc. All rights reserved. * * This source code is subject to the terms and conditions defined in the * file 'LICENSE' which is part of this source code package. * * Description: */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "common.h" #include "rc_list.h" #include "intf.h" #include "intf_uart.h" static rc_list_t rc_list[MAX_MAC_LIST] = {0}; static list_bt_t list_dev = {0}; static ssize_t rc_list_read(struct device *dev, struct device_attribute *attr, char *buf); static ssize_t rc_list_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); static void amlbt_wakeup_mutex(unsigned int flag) { list_bt_t *p_bt = &list_dev; if (flag) { // Wake up the system and prevent it from entering if (p_bt->amlbt_wakeup_source && (!p_bt->amlbt_wakeup_source->active)) { __pm_stay_awake(p_bt->amlbt_wakeup_source); } else { BTF("amlbt_wakeup_source is not initialized or active already\n"); } } else { if (p_bt->amlbt_wakeup_source && p_bt->amlbt_wakeup_source->active) { __pm_relax(p_bt->amlbt_wakeup_source); } else { BTF("amlbt_wakeup_source is not initialized or not active\n"); } } p_bt->wake_mux = flag; BTI("system state updated: %d\n", flag); } void amlbt_wakeup_lock(void) { int i = 0; list_bt_t *p_bt = &list_dev; if (!p_bt->wake_mux) { for (i = 0; i < MAX_MAC_LIST; i++) { if (rc_list[i].used) { amlbt_wakeup_mutex(1); break; } } } } void amlbt_wakeup_unlock(void) { list_bt_t *p_bt = &list_dev; if (p_bt->wake_mux) { amlbt_wakeup_mutex(0); } } static struct device_attribute dev_attr_rc_list = { .attr = { .name = AML_BT_CHAR_RCLIST_NAME, .mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH }, .show = rc_list_read, .store = rc_list_write, }; static void mac_to_str(char *buf, const char *mac) { sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x;", mac[0] & 0xFF, mac[1] & 0xFF, mac[2] & 0xFF, mac[3] & 0xFF, mac[4] & 0xFF, mac[5] & 0xFF); } static ssize_t rc_list_read(struct device *dev, struct device_attribute *attr, char *buf) { int i, len = 0; char mac_str[18]; for (i = 0; i < MAX_MAC_LIST; i++) { if (rc_list[i].used) { mac_to_str(mac_str, rc_list[i].mac); len += sprintf(buf + len, "%.18s\n", mac_str); } } return len; } static int amlbt_add_rc_list(char *mac) { unsigned int i = 0; for (i = 0; i < MAX_MAC_LIST; i++) { if (!rc_list[i].used) { BTP("amlbt_add_rc_list %d\n", i); rc_list[i].used = 1; memcpy(rc_list[i].mac, mac, MAC_ADDR_LEN); BTP("amlbt_add_rc_list success\n"); BTP("%#x\n",rc_list[i].mac[0]); BTP("%#x\n",rc_list[i].mac[1]); BTP("%#x\n",rc_list[i].mac[2]); BTP("%#x\n",rc_list[i].mac[3]); BTP("%#x\n",rc_list[i].mac[4]); BTP("%#x\n",rc_list[i].mac[5]); BTI("mac:[%#x,%#x,%#x,%#x,%#x,%#x]\n", rc_list[i].mac[0], rc_list[i].mac[1], rc_list[i].mac[2], rc_list[i].mac[3],rc_list[i].mac[4],rc_list[i].mac[5]); return 1; } } BTE("%s list full!!\n", __func__); return 0; } static ssize_t rc_list_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { char *mac_str = NULL; char *mac_token = NULL; char *mac_byte_str = NULL; char mac[MAC_ADDR_LEN] = {0}; unsigned int i = 0, j = 0; BTP("rc_list write count:%d\n", count); if (count > MAX_USER_BUF_LEN) { BTE("rc_list too long!:%d, %d\n", count, MAX_USER_BUF_LEN); amlbt_wakeup_unlock(); return -ENOMEM; } memset(rc_list, 0, sizeof(rc_list)); mac_str = (char *)buf; while ((mac_token = strsep(&mac_str, ";")) != NULL) { BTI("mac_token length: %d\n", strlen(mac_token)); if (strlen(mac_token) == 0) { BTW("NULL str\n"); continue; } if (strlen(mac_token) != MAC_ADDR_LEN * 2 + 5) { BTE("Invalid MAC address length: %s\n", mac_token); break; } for (i = 0, j = 0; i < MAC_ADDR_LEN; i++, j += 3) { mac_byte_str = &mac_token[j]; if (mac_token[j + 2] != ':' && j != 15) { BTE("Invalid MAC address format: %s\n", mac_token); return -EINVAL; } mac[i] = simple_strtoul(mac_byte_str, NULL, 16); } BTI("parsed mac: [%#x,%#x,%#x,%#x,%#x,%#x]\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); amlbt_add_rc_list(mac); amlbt_wakeup_unlock(); } return count; } /* static int amlbt_check_mac_exist(char *mac) { unsigned int i = 0; for (i = 0; i < MAX_MAC_LIST; i++) { if (rc_list[i].used && !memcmp(mac, rc_list[i].mac, MAC_ADDR_LEN)) { BTE("%s mac exist!!\n", __func__); return 1; } } return 0; } */ /* static int amlbt_del_rc_list(char *mac) { unsigned int i = 0; for (i = 0; i < MAX_MAC_LIST; i++) { if (rc_list[i].used && !memcmp(mac, rc_list[i].mac, MAC_ADDR_LEN)) { rc_list[i].used = 0; memset(rc_list[i].mac, 0, MAC_ADDR_LEN); return 1; } } BTE("%s rc addr not found!!\n", __func__); return 0; } */ int amlbt_write_rclist_to_firmware(void) { unsigned int i = 0; int ret = 0; char mac[256] = {0}; unsigned int cnt = 0; amlbt_t *p_bt = amlbt_intf_get_p_bt(); BTI("%s\n", __func__); for (i = 0; i < MAX_MAC_LIST; i++) { if (rc_list[i].used) { memcpy(&mac[i*MAC_ADDR_LEN], rc_list[i].mac, MAC_ADDR_LEN); BTP("%#x\n",mac[i*MAC_ADDR_LEN]); BTP("%#x\n",mac[i*MAC_ADDR_LEN+1]); BTP("%#x\n",mac[i*MAC_ADDR_LEN+2]); BTP("%#x\n",mac[i*MAC_ADDR_LEN+3]); BTP("%#x\n",mac[i*MAC_ADDR_LEN+4]); BTP("%#x\n",mac[i*MAC_ADDR_LEN+5]); BTP("mac:[%#x,%#x,%#x,%#x,%#x,%#x]\n", mac[i*MAC_ADDR_LEN], mac[i*MAC_ADDR_LEN+1], mac[i*MAC_ADDR_LEN+2], mac[i*MAC_ADDR_LEN+3],mac[i*MAC_ADDR_LEN+4],mac[i*MAC_ADDR_LEN+5]); cnt++; } } if (amlbt_intf_rw_get() == INTF_UART || p_bt->diag_res.chip_family_id == CHIP_W1D) { ret = amlbt_intf_vendor_write_rclist(mac, cnt, MAC_ADDR_LEN*cnt); if (ret != 0) { goto err_exit; } } else { mac[MAC_ADDR_LEN*MAX_MAC_LIST] = cnt; ret = amlbt_intf_write_sram(mac, FIFO_FW_RC_LIST_ADDR, MAC_ADDR_LEN*MAX_MAC_LIST+4); if (ret != 0) { goto err_exit; } } BTI("%s write rclist cnt:%d\n", __func__, cnt); if (amlbt_intf_rw_get() != INTF_UART && p_bt->diag_res.chip_family_id == CHIP_W2L) { memset(mac, 0, sizeof(mac)); ret = amlbt_intf_read_sram(mac, FIFO_FW_RC_LIST_ADDR, MAC_ADDR_LEN*MAX_MAC_LIST+4); if (ret != 0) { goto err_exit; } BTI("read rclist cnt:%#x\n", mac[MAC_ADDR_LEN*MAX_MAC_LIST]); } BTI("read rclist0:[%#x,%#x,%#x,%#x,%#x,%#x]\n", mac[0], mac[1], mac[2],mac[3],mac[4],mac[5]); BTI("read rclist1:[%#x,%#x,%#x,%#x,%#x,%#x]\n", mac[6], mac[7], mac[8],mac[9],mac[10],mac[11]); BTI("read rclist2:[%#x,%#x,%#x,%#x,%#x,%#x]\n", mac[12], mac[13], mac[14],mac[15],mac[16],mac[17]); BTI("read rclist3:[%#x,%#x,%#x,%#x,%#x,%#x]\n", mac[18], mac[19], mac[20],mac[21],mac[22],mac[23]); return ret; err_exit: return ret; } void amlbt_clear_rclist_from_firmware(void) { char mac[256] = {0}; int ret; unsigned int i = 0; amlbt_t *p_bt = amlbt_intf_get_p_bt(); if (amlbt_intf_rw_get() == INTF_UART || p_bt->diag_res.chip_family_id == CHIP_W1D) { ret = amlbt_intf_vendor_write_rclist(mac, MAX_MAC_LIST, MAC_ADDR_LEN*MAX_MAC_LIST); } else { ret = amlbt_intf_write_sram(mac, FIFO_FW_RC_LIST_ADDR, MAC_ADDR_LEN*MAX_MAC_LIST); } BTI("%s ret:%d\n", __func__, ret); } static void amlbt_register_wakeupsource(void) { #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) list_dev.amlbt_wakeup_source = wakeup_source_register("amlbt_wakeup_source"); #else list_dev.amlbt_wakeup_source = wakeup_source_register(NULL, "amlbt_wakeup_source"); #endif if (!list_dev.amlbt_wakeup_source) { BTE("Failed to create wakeup source\n"); return ; } } static void amlbt_unregister_wakeupsource(void) { if (list_dev.amlbt_wakeup_source) { wakeup_source_unregister(list_dev.amlbt_wakeup_source); list_dev.amlbt_wakeup_source = NULL; } else { BTE("amlbt_wakeup_source is not initialized, unregistering is not required.\n"); } } int amlbt_rc_list_init(struct device *dev) { int res = 0; BTI("%s \n", __func__); //BTI("%s, version:%s \n", __func__, AML_GD_VERSION); res = device_create_file(dev, &dev_attr_rc_list); if (res) { BTE("%s:Failed to create device attribute\n", __func__); } else { amlbt_register_wakeupsource(); } return res; } void amlbt_rc_list_deinit(struct device *dev) { amlbt_unregister_wakeupsource(); device_remove_file(dev, &dev_attr_rc_list); }