3304b13a2b
bes_chardev.c implemented a custom character device at /dev/bes2600 with
its own parser and command-dispatch table, exposing operations such as
'wifi on|off', 'bt on|off', 'change_fw_type <n>', 'bt_wakeup',
'bt_sleep', and 'wakeup_read_flag'. None of these surfaces are used by
the in-tree driver - every kernel call site consumes the internal state
accessors (bes2600_chrdev_is_signal_mode, bes2600_chrdev_get_fw_type,
etc) directly, not through the cdev.
The cdev interface is a standing upstream blocker for two reasons:
1. Drivers under drivers/staging/ and drivers/net/wireless/ are
expected to expose tuning via the firmware/nl80211/debugfs
infrastructure rather than a private /dev node with an ad-hoc
parser.
2. The cdev handlers keep a global bes_cdev singleton alive whose
->cdev, ->dev_id, ->class and ->device pointers exist only to be
torn down; they add no functionality that nl80211 or rfkill do
not already provide (wifi/bt on-off, module_param for fw_type).
Remove the userspace interface:
- open / read / write / release file_operations handlers and the
bes2600_chardev_fops instance
- bes2600_op_* command handlers and bes2600_op_map_tab dispatcher
- bes2600_get_cmd_and_ifname / bes2600_recyle_cmd_and_ifname_mem
string helpers
- bes2600_load_uevent (its only caller was
bes2600_chrdev_wifi_force_close_work informing userspace of a
state it already gates via rfkill; that snprintf +
kobject_uevent_env block is gone too, the kernel-side
halt_device + switch_wifi(0) + chrdev_check_system_close
sequence remains)
- alloc_chrdev_region / cdev_init / cdev_add / class_create /
device_create in bes2600_chrdev_init plus the fail1/fail2/fail3
unwind labels
- cdev_del / unregister_chrdev_region / device_destroy /
class_destroy in bes2600_chrdev_free
- cdev/dev_id/major/minor/class/device fields in struct bes_cdev
What remains (unchanged behaviour):
- fw_type module parameter - the primary user-facing knob for
signal/no-signal/BT mode switch
- All in-kernel bes2600_chrdev_* accessor functions called from
bes2600_sdio.c, bes_pwr.c, sta.c, bh.c, main.c, wsm.c, and
wifi_testmode_cmd.c (13 call sites)
- bes2600_chrdev_init / bes2600_chrdev_free as state-init / teardown
for the remaining bes_cdev state (waitqueues, workqueues, flags)
- DPD management (bes2600_chrdev_get_dpd_buffer / update / free)
- wifi_force_close worker, system-close logic, bus-probe state
machine
Tested-on: PineTab2 (BES2600WM + RK3566) running linux-pinetab2
6.19.10-danctnix1-1. Driver continues to associate and pass traffic;
no kernel messages related to the cdev absence. Users that previously
wrote to /dev/bes2600 should switch to the fw_type module parameter
or (future patch c4) nl80211 testmode commands.
Follow-ups:
- c3.1: thread struct device * through bes2600_chrdev_is_signal_mode
and friends so the global bes2600_cdev singleton can be dropped
and the accessors scale to multi-device scenarios.
- c4: enable CONFIG_BES2600_TESTMODE and route nl80211 testmode
commands to the firmware's patch_wifi_testMode entry.
Signed-off-by: Markus Fritsche <fritsche.markus@gmail.com>
826 lines
20 KiB
C
826 lines
20 KiB
C
/*
|
|
* Mac80211 driver for BES2600 device
|
|
*
|
|
* Copyright (c) 2022, Bestechnic
|
|
* Author:
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*/
|
|
#include<linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/firmware.h>
|
|
#include <linux/etherdevice.h>
|
|
#include <linux/vmalloc.h>
|
|
#include <linux/random.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/cdev.h>
|
|
#include <linux/atomic.h>
|
|
#include <linux/wait.h>
|
|
#include <linux/crc32.h>
|
|
#include <linux/version.h>
|
|
#include "bes2600.h"
|
|
#include "sbus.h"
|
|
#include "hwio.h"
|
|
#include "fwio.h"
|
|
#include "bes_chardev.h"
|
|
#include "tx_loop.h"
|
|
#include "bes_log.h"
|
|
|
|
enum wait_state {
|
|
BES2600_BOOT_WAIT_NONE = 0,
|
|
BES2600_BOOT_WAIT_PROBE_DONE,
|
|
BES2600_BOOT_WAIT_CLOSE,
|
|
};
|
|
|
|
enum bus_probe_state {
|
|
BES2600_BUS_PROBE_NONE = 0,
|
|
BES2600_BUS_PROBE_START,
|
|
BES2600_BUS_PROBE_OK,
|
|
BES2600_BUS_PROBE_TIMEOUT,
|
|
};
|
|
|
|
struct bes_cdev {
|
|
atomic_t num_proc;
|
|
wait_queue_head_t open_wq;
|
|
spinlock_t status_lock;
|
|
bool wifi_opened;
|
|
bool bt_opened;
|
|
bool bton_pending;
|
|
bool dpd_calied;
|
|
u8 *dpd_data;
|
|
u32 dpd_len;
|
|
enum wait_state wait_state;
|
|
wait_queue_head_t probe_done_wq;
|
|
const struct sbus_ops *sbus_ops;
|
|
struct sbus_priv *sbus_priv;
|
|
bool sig_mode;
|
|
int fw_type;
|
|
bool bus_error;
|
|
bool halt_dev;
|
|
struct delayed_work probe_timeout_work;
|
|
enum bus_probe_state bus_probe;
|
|
struct work_struct wifi_force_close_work;
|
|
#ifdef BES2600_WRITE_DPD_TO_FILE
|
|
int no_dpd;
|
|
#endif
|
|
enum pend_read_op read_flag;
|
|
enum wakeup_event wakeup_by_event; /* used to filter unwanted event wakeup reason report */
|
|
u16 wakeup_state; /* for userspace check wakeup reason */
|
|
wait_queue_head_t wakeup_reason_wq;
|
|
u16 src_port;
|
|
#ifdef BES2600_DUMP_FW_DPD_LOG
|
|
u8 *dpd_log;
|
|
u16 dpd_log_len;
|
|
#endif
|
|
};
|
|
|
|
struct bes2600_op_map {
|
|
char op[20]; // operation
|
|
int op_len; // operation length, used for effiency
|
|
int (*handler)(const char *cmd); // handler
|
|
};
|
|
|
|
static struct bes_cdev bes2600_cdev;
|
|
module_param_named(fw_type, bes2600_cdev.fw_type, int, 0644);
|
|
#ifdef BES2600_WRITE_DPD_TO_FILE
|
|
module_param_named(no_dpd, bes2600_cdev.no_dpd, int, 0644);
|
|
#endif
|
|
|
|
extern int bes2600_register_net_dev(struct sbus_priv *bus_priv);
|
|
extern int bes2600_unregister_net_dev(struct sbus_priv *bus_priv);
|
|
extern bool bes2600_is_net_dev_created(struct sbus_priv *bus_priv);
|
|
|
|
static bool bes2600_bootup_end(void)
|
|
{
|
|
bool end;
|
|
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
end = (bes2600_cdev.bus_probe == BES2600_BUS_PROBE_TIMEOUT ||
|
|
bes2600_cdev.sbus_priv != NULL ||
|
|
bes2600_cdev.bus_error);
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
return end;
|
|
}
|
|
|
|
static int bes2600_chrdev_switch_subsys(int wake_flag, int subsys, bool active)
|
|
{
|
|
int ret = 0;
|
|
|
|
if (bes2600_cdev.sbus_priv == NULL)
|
|
return -EFAULT;
|
|
|
|
if (active) {
|
|
if (bes2600_cdev.sbus_ops->gpio_wake)
|
|
bes2600_cdev.sbus_ops->gpio_wake(bes2600_cdev.sbus_priv, wake_flag);
|
|
|
|
if (bes2600_cdev.sbus_ops->sbus_active)
|
|
ret = bes2600_cdev.sbus_ops->sbus_active(bes2600_cdev.sbus_priv, subsys);
|
|
|
|
if (bes2600_cdev.sbus_ops->gpio_sleep)
|
|
bes2600_cdev.sbus_ops->gpio_sleep(bes2600_cdev.sbus_priv, wake_flag);
|
|
} else {
|
|
if (bes2600_cdev.sbus_ops->gpio_wake)
|
|
bes2600_cdev.sbus_ops->gpio_wake(bes2600_cdev.sbus_priv, wake_flag);
|
|
|
|
if (bes2600_cdev.sbus_ops->sbus_deactive)
|
|
ret = bes2600_cdev.sbus_ops->sbus_deactive(bes2600_cdev.sbus_priv, subsys);
|
|
|
|
if (bes2600_cdev.sbus_ops->gpio_sleep)
|
|
bes2600_cdev.sbus_ops->gpio_sleep(bes2600_cdev.sbus_priv, wake_flag);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int bes2600_switch_wifi(bool on)
|
|
{
|
|
int ret = 0;
|
|
long status = 0;
|
|
|
|
if (bes2600_cdev.wifi_opened == on)
|
|
return 0;
|
|
|
|
if (on) {
|
|
if (bes2600_chrdev_check_system_close()) {
|
|
bes_devel("power up bes2600 when active wifi.\n");
|
|
/* reset bus error status when restart bes2600 */
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.bus_error = false;
|
|
bes2600_cdev.halt_dev = false;
|
|
bes2600_cdev.bus_probe = BES2600_BUS_PROBE_NONE;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
/* power up bes2600, trigger system to execute probe function */
|
|
bes2600_cdev.wifi_opened = true;
|
|
bes2600_cdev.sbus_ops->power_switch(NULL, 1);
|
|
|
|
/* wait probe done event */
|
|
status = wait_event_timeout(bes2600_cdev.probe_done_wq,
|
|
bes2600_bootup_end(), HZ * 8);
|
|
WARN_ON(status <= 0);
|
|
ret = (status <= 0 || bes2600_chrdev_is_bus_error()) ? -1 : 0;
|
|
} else {
|
|
/* bes2600 is already powered up, we just need to create net device */
|
|
if (bes2600_cdev.sbus_priv) {
|
|
if (!bes2600_is_net_dev_created(bes2600_cdev.sbus_priv)) {
|
|
ret = bes2600_register_net_dev(bes2600_cdev.sbus_priv);
|
|
}
|
|
} else {
|
|
ret = -EFAULT;
|
|
}
|
|
}
|
|
} else {
|
|
if (bes2600_cdev.sbus_priv && bes2600_is_net_dev_created(bes2600_cdev.sbus_priv)) {
|
|
bes2600_unregister_net_dev(bes2600_cdev.sbus_priv);
|
|
}
|
|
}
|
|
|
|
if (!ret) {
|
|
bes2600_cdev.wifi_opened = on;
|
|
} else {
|
|
bes2600_cdev.wifi_opened = false;
|
|
if (on)
|
|
bes_info("open wifi failed\n");
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int bes2600_switch_bt(bool on)
|
|
{
|
|
int ret = 0;
|
|
long status = 0;
|
|
|
|
if (bes2600_cdev.bt_opened == on)
|
|
return 0;
|
|
|
|
if (on) {
|
|
if (bes2600_chrdev_check_system_close()) {
|
|
bes_devel("power up bes2600 when active bt.\n");
|
|
/* reset bus error status when restart bes2600 */
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.bus_error = false;
|
|
bes2600_cdev.halt_dev = false;
|
|
bes2600_cdev.bus_probe = BES2600_BUS_PROBE_NONE;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
/* set opend state in advance */
|
|
bes2600_cdev.bt_opened = true;
|
|
bes2600_cdev.bton_pending = true;
|
|
|
|
/* power up bes2600, trigger system to execute probe function */
|
|
bes2600_cdev.sbus_ops->power_switch(NULL, 1);
|
|
|
|
/* wait bootup process end */
|
|
status = wait_event_timeout(bes2600_cdev.probe_done_wq,
|
|
bes2600_bootup_end(), HZ * 8);
|
|
WARN_ON(status <= 0);
|
|
|
|
/* check if there is a error when bootup */
|
|
ret = (status <= 0 || bes2600_chrdev_is_bus_error()) ? -1 : 0;
|
|
} else {
|
|
bes_info("enable BT\n");
|
|
ret = bes2600_chrdev_switch_subsys(GPIO_WAKE_FLAG_BT_ON, SUBSYSTEM_BT, true);
|
|
}
|
|
} else {
|
|
bes_info("disable BT\n");
|
|
bes2600_chrdev_switch_subsys(GPIO_WAKE_FLAG_BT_OFF, SUBSYSTEM_BT, false);
|
|
}
|
|
|
|
if (!ret) {
|
|
bes2600_cdev.bt_opened = on;
|
|
} else {
|
|
bes2600_cdev.bt_opened = false;
|
|
bes2600_cdev.bton_pending = false;
|
|
if (ret)
|
|
bes_info("open bt failed\n");
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef FW_DOWNLOAD_UART_DAEMON
|
|
#endif
|
|
|
|
|
|
static int bes2600_chrdev_check_system_close_internal(void)
|
|
{
|
|
return (bes2600_cdev.sbus_ops->power_switch != NULL &&
|
|
bes2600_cdev.fw_type == BES2600_FW_TYPE_WIFI_SIGNAL)
|
|
&&(bes2600_cdev.bt_opened == false)
|
|
&& (bes2600_cdev.wifi_opened == false);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef BES2600_WRITE_DPD_TO_FILE
|
|
static int bes2600_chrdev_write_dpd_data_to_file(const char *path, void *buffer, int size)
|
|
{
|
|
int ret = 0;
|
|
struct file *fp;
|
|
|
|
if (buffer == NULL || size == 0)
|
|
return 0;
|
|
|
|
fp = filp_open(path, O_TRUNC | O_CREAT | O_RDWR, S_IRUSR);
|
|
if (IS_ERR(fp)) {
|
|
bes_err("BES2600 : can't open %s\n",path);
|
|
return -1;
|
|
}
|
|
|
|
ret = kernel_write(fp, buffer, size, &fp->f_pos);
|
|
if (ret < 0)
|
|
bes_err("write dpd to file failed\n");
|
|
|
|
filp_close(fp,NULL);
|
|
|
|
bes_devel("write dpd to %s\n", path);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static bool bes2600_chrdev_dpd_is_vaild(u8 *dpd_data)
|
|
{
|
|
u32 cal_crc = 0;
|
|
u32 dpd_crc = le32_to_cpup((__le32 *)(dpd_data));
|
|
u32 dpd_ver = le32_to_cpup((__le32 *)(dpd_data + DPD_VERSION_OFFSET));
|
|
|
|
/* check version */
|
|
if (dpd_ver < DPD_CUR_VERSION)
|
|
return false;
|
|
|
|
cal_crc ^= 0xffffffffL;
|
|
cal_crc = crc32_le(cal_crc, dpd_data + 4, DPD_BIN_SIZE - 4);
|
|
cal_crc ^= 0xffffffffL;
|
|
|
|
/* check if the dpd data is valid */
|
|
if (cal_crc != dpd_crc) {
|
|
bes_err(
|
|
"bes2600 dpd data from file check failed, calc_crc:0x%08x dpd_crc: 0x%08x\n",
|
|
cal_crc, dpd_crc);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static int bes2600_chrdev_read_and_check_dpd_data(const char *file, u8 **data, u32 *len)
|
|
{
|
|
int ret = 0;
|
|
u8* read_data = NULL;
|
|
struct file *fp;
|
|
|
|
/* open file */
|
|
fp = filp_open(file, O_RDONLY, 0);//S_IRUSR
|
|
if (IS_ERR(fp)) {
|
|
bes_devel("BES2600 : can't open %s\n",file);
|
|
return -1;
|
|
}
|
|
|
|
#ifdef BES2600_WRITE_DPD_TO_FILE
|
|
if (fp->f_inode->i_size != DPD_BIN_FILE_SIZE) {
|
|
bes_err(
|
|
"bes2600 dpd data file size check failed, read_size: %lld file_size: %d\n",
|
|
fp->f_inode->i_size, DPD_BIN_FILE_SIZE);
|
|
filp_close(fp, NULL);
|
|
return -1;
|
|
}
|
|
#endif
|
|
|
|
/* allocate memory for storing reading data */
|
|
read_data = kmalloc(fp->f_inode->i_size, GFP_KERNEL);
|
|
if (read_data == NULL) {
|
|
bes_devel("%s alloc mem fail\n", __func__);
|
|
goto err1;
|
|
}
|
|
|
|
/* read data from file */
|
|
ret = kernel_read(fp, read_data, fp->f_inode->i_size, &fp->f_pos);
|
|
if (ret < DPD_BIN_SIZE) {
|
|
bes_err("%s read fail, ret=%d\n", __func__, ret);
|
|
goto err2;
|
|
}
|
|
|
|
/* check dpd version and crc */
|
|
if (!bes2600_chrdev_dpd_is_vaild(read_data))
|
|
goto err2;
|
|
|
|
/* close file */
|
|
filp_close(fp, NULL);
|
|
|
|
/* copy data to external */
|
|
*data = read_data;
|
|
*len = DPD_BIN_SIZE;;
|
|
|
|
/* output debug information */
|
|
bes_devel("read dpd data from %s\n", file);
|
|
|
|
return 0;
|
|
|
|
err2:
|
|
kfree(read_data);
|
|
err1:
|
|
filp_close(fp, NULL);
|
|
*data = NULL;
|
|
*len = 0;
|
|
return -1;
|
|
}
|
|
#endif
|
|
|
|
const u8* bes2600_chrdev_get_dpd_data(u32 *len)
|
|
{
|
|
#ifdef BES2600_WRITE_DPD_TO_FILE
|
|
if (!bes2600_cdev.dpd_calied && bes2600_cdev.no_dpd) {
|
|
/* read dpd data from file that stores factory dpd calibration data */
|
|
if ((bes2600_chrdev_read_and_check_dpd_data(BES2600_DPD_GOLDEN_PATH,
|
|
&bes2600_cdev.dpd_data, &bes2600_cdev.dpd_len) < 0) &&
|
|
(bes2600_chrdev_read_and_check_dpd_data(BES2600_DEFAULT_DPD_PATH,
|
|
&bes2600_cdev.dpd_data, &bes2600_cdev.dpd_len) < 0)) {
|
|
bes_err("%s read dpd data fail\n", __func__);
|
|
return NULL;
|
|
} else {
|
|
bes2600_cdev.dpd_calied = true;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
if (!bes2600_cdev.dpd_calied)
|
|
return NULL;
|
|
if (len)
|
|
*len = bes2600_cdev.dpd_len;
|
|
|
|
return bes2600_cdev.dpd_data;
|
|
}
|
|
|
|
u8* bes2600_chrdev_get_dpd_buffer(u32 size)
|
|
{
|
|
if (bes2600_cdev.dpd_data)
|
|
kfree(bes2600_cdev.dpd_data);
|
|
|
|
bes2600_cdev.dpd_data = kmalloc(size, GFP_KERNEL);
|
|
if (!bes2600_cdev.dpd_data) {
|
|
return NULL;
|
|
}
|
|
|
|
bes2600_cdev.dpd_len = DPD_BIN_SIZE;
|
|
|
|
return bes2600_cdev.dpd_data;
|
|
}
|
|
|
|
void bes2600_chrdev_free_dpd_data(void)
|
|
{
|
|
if (bes2600_cdev.dpd_data)
|
|
kfree(bes2600_cdev.dpd_data);
|
|
|
|
bes2600_cdev.dpd_data = NULL;
|
|
bes2600_cdev.dpd_len = 0;
|
|
}
|
|
|
|
int bes2600_chrdev_update_dpd_data(void)
|
|
{
|
|
u32 cal_crc = 0;
|
|
u32 dpd_crc = le32_to_cpup((__le32 *)(bes2600_cdev.dpd_data));
|
|
|
|
/* check if the dpd data is valid */
|
|
cal_crc ^= 0xffffffffL;
|
|
cal_crc = crc32_le(cal_crc, bes2600_cdev.dpd_data + 4, bes2600_cdev.dpd_len - 4);
|
|
cal_crc ^= 0xffffffffL;
|
|
if (cal_crc != dpd_crc) {
|
|
bes_err(
|
|
"bes2600 dpd data check failed, calc_crc:0x%08x dpd_crc: 0x%08x\n",
|
|
cal_crc, dpd_crc);
|
|
return -1;
|
|
}
|
|
|
|
bes_devel("bes2600 dpd cali pass.\n");
|
|
|
|
/* update dpd calibration and wait state */
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.dpd_calied = true;
|
|
if (bes2600_chrdev_check_system_close_internal()) {
|
|
bes2600_cdev.wait_state = BES2600_BOOT_WAIT_CLOSE;
|
|
} else {
|
|
bes2600_cdev.wait_state = BES2600_BOOT_WAIT_PROBE_DONE;
|
|
}
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
#ifdef BES2600_WRITE_DPD_TO_FILE
|
|
/* write dpd data to file */
|
|
memset(bes2600_cdev.dpd_data + DPD_BIN_SIZE, 0, DPD_BIN_FILE_SIZE - DPD_BIN_SIZE);
|
|
bes2600_chrdev_write_dpd_data_to_file(BES2600_DPD_PATH,
|
|
bes2600_cdev.dpd_data, DPD_BIN_FILE_SIZE);
|
|
#endif
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
#ifdef BES2600_DUMP_FW_DPD_LOG
|
|
void bes2600_free_dpd_log_buffer(void)
|
|
{
|
|
if (bes2600_cdev.dpd_log)
|
|
kfree(bes2600_cdev.dpd_log);
|
|
|
|
bes2600_cdev.dpd_log_len = 0;
|
|
bes2600_cdev.dpd_log = NULL;
|
|
}
|
|
|
|
u8* bes2600_alloc_dpd_log_buffer(u16 len)
|
|
{
|
|
bes2600_cdev.dpd_log_len = len;
|
|
|
|
if (bes2600_cdev.dpd_log)
|
|
kfree(bes2600_cdev.dpd_log);
|
|
|
|
bes2600_cdev.dpd_log = kzalloc(len, GFP_KERNEL);
|
|
if (!bes2600_cdev.dpd_log) {
|
|
bes2600_cdev.dpd_log_len = 0;
|
|
return NULL;
|
|
}
|
|
|
|
return bes2600_cdev.dpd_log;
|
|
}
|
|
|
|
void bes2600_get_dpd_log(char **data, size_t *len)
|
|
{
|
|
if (!bes2600_cdev.dpd_log) {
|
|
*data = NULL;
|
|
*len = 0;
|
|
} else {
|
|
*data = bes2600_cdev.dpd_log;
|
|
*len = (size_t)bes2600_cdev.dpd_log_len;
|
|
}
|
|
}
|
|
#endif /* BES2600_DUMP_FW_DPD_LOG */
|
|
|
|
void bes2600_chrdev_set_sbus_priv_data(struct sbus_priv *priv, bool error)
|
|
{
|
|
bes2600_cdev.sbus_priv = priv;
|
|
if (priv) {
|
|
if (bes2600_cdev.bton_pending) {
|
|
bes_devel("execute pending bt on operation.\n");
|
|
bes2600_chrdev_switch_subsys(GPIO_WAKE_FLAG_BT_ON, SUBSYSTEM_BT, true);
|
|
|
|
bes2600_cdev.bton_pending = false;
|
|
}
|
|
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
if (bes2600_cdev.wait_state == BES2600_BOOT_WAIT_PROBE_DONE) {
|
|
bes2600_cdev.wait_state = BES2600_BOOT_WAIT_NONE;
|
|
}
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
bes_devel("wakup proc on wq of probe_done.\n");
|
|
} else {
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.wait_state = BES2600_BOOT_WAIT_NONE;
|
|
bes2600_cdev.bus_error = error;
|
|
if (bes2600_cdev.bus_error) {
|
|
bes2600_cdev.wifi_opened = false;
|
|
bes2600_cdev.bt_opened = false;
|
|
bes2600_cdev.bton_pending = false;
|
|
bes2600_cdev.bus_probe = BES2600_BUS_PROBE_NONE;
|
|
}
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
bes_devel("wakup proc on wq of disconnect_done.\n");
|
|
}
|
|
|
|
wake_up(&bes2600_cdev.probe_done_wq);
|
|
}
|
|
|
|
struct sbus_priv * bes2600_chrdev_get_sbus_priv_data(void)
|
|
{
|
|
return bes2600_cdev.sbus_priv;
|
|
}
|
|
|
|
int bes2600_chrdev_check_system_close(void)
|
|
{
|
|
bool sys_closed = false;
|
|
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
sys_closed = bes2600_chrdev_check_system_close_internal();
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
return sys_closed;
|
|
}
|
|
|
|
int bes2600_chrdev_do_system_close(const struct sbus_ops *sbus_ops, struct sbus_priv *priv)
|
|
{
|
|
int ret = 0;
|
|
long status = 0;
|
|
|
|
if (!sbus_ops || !priv) {
|
|
bes_warn("abort power down device.\n");
|
|
return 0;
|
|
}
|
|
|
|
if (!sbus_ops->power_switch)
|
|
return 0;
|
|
|
|
bes_devel("power down bes2600.\n");
|
|
|
|
/* trigger system to execute disconnect function */
|
|
ret = sbus_ops->power_switch(priv, 0);
|
|
|
|
/* wait disconnect event */
|
|
status = wait_event_timeout(bes2600_cdev.probe_done_wq, (bes2600_cdev.sbus_priv == NULL), HZ * 3);
|
|
WARN_ON(status <= 0);
|
|
|
|
return ret;
|
|
}
|
|
|
|
bool bes2600_chrdev_is_wifi_opened(void)
|
|
{
|
|
bool wifi_opened = false;
|
|
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
wifi_opened = bes2600_cdev.wifi_opened;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
if (bes2600_cdev.fw_type == BES2600_FW_TYPE_WIFI_NO_SIGNAL)
|
|
return true;
|
|
else if (bes2600_cdev.fw_type == BES2600_FW_TYPE_BT)
|
|
return false;
|
|
else if (bes2600_cdev.fw_type == BES2600_FW_TYPE_WIFI_SIGNAL)
|
|
return wifi_opened;
|
|
|
|
return false;
|
|
}
|
|
|
|
bool bes2600_chrdev_is_bt_opened(void)
|
|
{
|
|
bool bt_opened = false;
|
|
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bt_opened = bes2600_cdev.bt_opened;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
if (bes2600_cdev.fw_type == BES2600_FW_TYPE_WIFI_NO_SIGNAL)
|
|
return false;
|
|
else if (bes2600_cdev.fw_type == BES2600_FW_TYPE_BT)
|
|
return true;
|
|
else if (bes2600_cdev.fw_type == BES2600_FW_TYPE_WIFI_SIGNAL)
|
|
return bt_opened;
|
|
|
|
return false;
|
|
}
|
|
|
|
void bes2600_chrdev_wakeup_bt(void)
|
|
{
|
|
int ret = 0;
|
|
|
|
if (bes2600_cdev.bt_opened && bes2600_cdev.sbus_priv) {
|
|
bes_devel("wakeup bt in resume flow\n");
|
|
ret = bes2600_chrdev_switch_subsys(GPIO_WAKE_FLAG_BT_LP_ON, SUBSYSTEM_BT_LP, true);
|
|
|
|
if (ret)
|
|
bes_err("Wakeup BT fail in resume\n");
|
|
}
|
|
}
|
|
|
|
int bes2600_chrdev_get_fw_type(void)
|
|
{
|
|
return bes2600_cdev.fw_type;
|
|
}
|
|
|
|
bool bes2600_chrdev_is_signal_mode(void)
|
|
{
|
|
return bes2600_cdev.sig_mode;
|
|
}
|
|
|
|
bool bes2600_chrdev_is_bus_error(void)
|
|
{
|
|
bool error = false;
|
|
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
error = (bes2600_cdev.bus_error || bes2600_cdev.bus_probe != BES2600_BUS_PROBE_OK);
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
return error;
|
|
}
|
|
|
|
void bes2600_chrdev_update_signal_mode(void)
|
|
{
|
|
if (bes2600_cdev.fw_type >= BES2600_FW_TYPE_MAX_NUM) {
|
|
bes2600_cdev.fw_type = BES2600_FW_TYPE_WIFI_SIGNAL;
|
|
bes_warn("unexpected fw type, switch to wifi signal mode\n");
|
|
}
|
|
|
|
if (bes2600_cdev.fw_type == BES2600_FW_TYPE_WIFI_SIGNAL) {
|
|
bes2600_cdev.sig_mode = true;
|
|
} else if ((bes2600_cdev.fw_type == BES2600_FW_TYPE_WIFI_NO_SIGNAL)
|
|
|| (bes2600_cdev.fw_type == BES2600_FW_TYPE_BT)) {
|
|
bes2600_cdev.sig_mode = false;
|
|
}
|
|
}
|
|
|
|
static void bes2600_chrdev_wifi_force_close_work(struct work_struct *work)
|
|
{
|
|
if (bes2600_chrdev_is_wifi_opened()) {
|
|
bes_devel("system exeception, force wifi down\n");
|
|
|
|
/* halt device if needed */
|
|
if (bes2600_cdev.halt_dev && bes2600_cdev.sbus_ops->halt_device) {
|
|
bes2600_cdev.sbus_ops->halt_device(bes2600_cdev.sbus_priv);
|
|
}
|
|
|
|
/* unregister wifi */
|
|
bes2600_switch_wifi(0);
|
|
|
|
/* power down device if wifi is only opened */
|
|
if (bes2600_chrdev_check_system_close()) {
|
|
bes2600_chrdev_do_system_close(bes2600_cdev.sbus_ops,
|
|
bes2600_cdev.sbus_priv);
|
|
}
|
|
}
|
|
}
|
|
|
|
void bes2600_chrdev_wifi_force_close(struct bes2600_common *hw_priv, bool halt_dev)
|
|
{
|
|
if (hw_priv == NULL)
|
|
return;
|
|
|
|
if (bes2600_chrdev_is_wifi_opened() &&
|
|
!work_pending(&bes2600_cdev.wifi_force_close_work)) {
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.bus_error = true;
|
|
bes2600_cdev.halt_dev = halt_dev;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
bes2600_tx_loop_set_enable(hw_priv, true);
|
|
schedule_work(&bes2600_cdev.wifi_force_close_work);
|
|
}
|
|
}
|
|
|
|
void bes2600_chrdev_usb_remove(struct bes2600_common *hw_priv)
|
|
{
|
|
if (hw_priv == NULL)
|
|
return;
|
|
|
|
if (bes2600_chrdev_is_wifi_opened() &&
|
|
!work_pending(&bes2600_cdev.wifi_force_close_work)) {
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.bus_error = true;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
bes2600_tx_loop_set_enable(hw_priv, false);
|
|
bes2600_chrdev_wifi_force_close_work(&bes2600_cdev.wifi_force_close_work);
|
|
}
|
|
}
|
|
|
|
static void bes2600_probe_timeout_work(struct work_struct *work)
|
|
{
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.bus_probe = BES2600_BUS_PROBE_TIMEOUT;
|
|
bes2600_cdev.wifi_opened = false;
|
|
bes2600_cdev.bt_opened = false;
|
|
bes2600_cdev.bton_pending = false;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
bes_devel("bus probe timeout\n");
|
|
wake_up(&bes2600_cdev.probe_done_wq);
|
|
}
|
|
|
|
void bes2600_chrdev_start_bus_probe(void)
|
|
{
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.bus_probe = BES2600_BUS_PROBE_START;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
|
|
cancel_delayed_work_sync(&bes2600_cdev.probe_timeout_work);
|
|
schedule_delayed_work(&bes2600_cdev.probe_timeout_work, 3 * HZ);
|
|
}
|
|
|
|
void bes2600_chrdev_bus_probe_notify(void)
|
|
{
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.bus_probe = BES2600_BUS_PROBE_OK;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
cancel_delayed_work_sync(&bes2600_cdev.probe_timeout_work);
|
|
|
|
wake_up(&bes2600_cdev.probe_done_wq);
|
|
}
|
|
|
|
void bes2600_chrdev_wifi_update_wakeup_reason(u16 reason, u16 port)
|
|
{
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.wakeup_state = reason;
|
|
bes2600_cdev.src_port = port;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
}
|
|
|
|
void bes2600_chrdev_wakeup_by_event_set(enum wakeup_event wakeup_event)
|
|
{
|
|
spin_lock(&bes2600_cdev.status_lock);
|
|
bes2600_cdev.wakeup_by_event = wakeup_event;
|
|
spin_unlock(&bes2600_cdev.status_lock);
|
|
if (wakeup_event == WAKEUP_EVENT_NONE)
|
|
wake_up(&bes2600_cdev.wakeup_reason_wq);
|
|
}
|
|
|
|
int bes2600_chrdev_wakeup_by_event_get(void)
|
|
{
|
|
return bes2600_cdev.wakeup_by_event;
|
|
}
|
|
|
|
int bes2600_chrdev_init(struct sbus_ops *ops)
|
|
{
|
|
/* initialise global variable */
|
|
atomic_set(&bes2600_cdev.num_proc, 0);
|
|
init_waitqueue_head(&bes2600_cdev.open_wq);
|
|
spin_lock_init(&bes2600_cdev.status_lock);
|
|
init_waitqueue_head(&bes2600_cdev.probe_done_wq);
|
|
INIT_WORK(&bes2600_cdev.wifi_force_close_work, bes2600_chrdev_wifi_force_close_work);
|
|
INIT_DELAYED_WORK(&bes2600_cdev.probe_timeout_work, bes2600_probe_timeout_work);
|
|
init_waitqueue_head(&bes2600_cdev.wakeup_reason_wq);
|
|
bes2600_chrdev_wakeup_by_event_set(WAKEUP_EVENT_NONE);
|
|
#ifdef CONFIG_BES2600_WIFI_BOOT_ON
|
|
bes2600_cdev.wifi_opened = true;
|
|
#else
|
|
bes2600_cdev.wifi_opened = false;
|
|
#endif
|
|
#ifdef CONFIG_BES2600_BT_BOOT_ON
|
|
bes2600_cdev.bt_opened = true;
|
|
bes2600_cdev.bton_pending = true;
|
|
#else
|
|
bes2600_cdev.bt_opened = false;
|
|
bes2600_cdev.bton_pending = false;
|
|
#endif
|
|
bes2600_cdev.dpd_calied = false;
|
|
bes2600_cdev.wait_state = BES2600_BOOT_WAIT_NONE;
|
|
bes2600_cdev.sbus_ops = ops;
|
|
bes2600_cdev.bus_error = false;
|
|
bes2600_cdev.halt_dev = false;
|
|
bes2600_cdev.read_flag = BES_CDEV_READ_NUM_MAX;
|
|
bes2600_cdev.wakeup_by_event = WAKEUP_EVENT_NONE;
|
|
bes_devel("%s done\n", __func__);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void bes2600_chrdev_free(void)
|
|
{
|
|
cancel_delayed_work_sync(&bes2600_cdev.probe_timeout_work);
|
|
#ifdef BES2600_DUMP_FW_DPD_LOG
|
|
bes2600_free_dpd_log_buffer();
|
|
#endif
|
|
bes2600_chrdev_free_dpd_data();
|
|
bes_devel("%s done\n", __func__);
|
|
}
|