2017-04-23 22:17:43 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/wireless/ieee80211/bcmf_cdc.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Simon Piriou <spiriou31@gmail.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
|
|
|
|
#include "bcmf_driver.h"
|
|
|
|
#include "bcmf_ioctl.h"
|
|
|
|
#include "bcmf_utils.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
/* Control header flags */
|
|
|
|
|
|
|
|
#define BCMF_CONTROL_ERROR 0x01 /* Command failure flag */
|
|
|
|
#define BCMF_CONTROL_SET 0x02 /* Command type: SET = 1, GET = 0 */
|
|
|
|
#define BCMF_CONTROL_INTERFACE_SHIFT 12
|
|
|
|
#define BCMF_CONTROL_REQID_SHIFT 16
|
|
|
|
|
|
|
|
#define BCMF_CONTROL_TIMEOUT_MS 1000
|
2017-04-23 22:17:43 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
struct __attribute__((packed)) bcmf_cdc_header {
|
|
|
|
uint32_t cmd; /* Command to be sent */
|
|
|
|
uint32_t len; /* Size of command data */
|
|
|
|
uint32_t flags; /* cdc request flags, see above */
|
|
|
|
uint32_t status; /* Returned status code from chip */
|
2017-04-23 22:17:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
static struct bcmf_frame_s *bcmf_cdc_allocate_frame(
|
2017-04-23 22:17:43 +02:00
|
|
|
FAR struct bcmf_dev_s *priv, char *name,
|
|
|
|
uint8_t *data, uint32_t len);
|
|
|
|
|
|
|
|
static int bcmf_cdc_sendframe(FAR struct bcmf_dev_s *priv, uint32_t cmd,
|
|
|
|
int ifidx, bool set, struct bcmf_frame_s *frame);
|
|
|
|
|
|
|
|
static int bcmf_cdc_control_request(FAR struct bcmf_dev_s *priv,
|
|
|
|
uint32_t ifidx, bool set, uint32_t cmd,
|
|
|
|
char *name, uint8_t *data, uint32_t *len);
|
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
static int bcmf_cdc_control_request_unsafe(FAR struct bcmf_dev_s *priv,
|
|
|
|
uint32_t ifidx, bool set, uint32_t cmd,
|
|
|
|
char *name, uint8_t *data, uint32_t *len);
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
struct bcmf_frame_s *bcmf_cdc_allocate_frame(FAR struct bcmf_dev_s *priv,
|
|
|
|
char *name, uint8_t *data,
|
|
|
|
uint32_t len)
|
2017-04-23 22:17:43 +02:00
|
|
|
{
|
|
|
|
uint32_t data_len;
|
|
|
|
uint16_t name_len;
|
|
|
|
struct bcmf_frame_s *frame;
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
name_len = strlen(name) + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
name_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
data_len = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate control frame */
|
|
|
|
|
|
|
|
frame = priv->bus->allocate_frame(priv,
|
|
|
|
sizeof(struct bcmf_cdc_header) + data_len + name_len,
|
|
|
|
true, true);
|
2017-05-04 02:30:40 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (!frame)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy name string and data */
|
|
|
|
|
|
|
|
memcpy(frame->data + sizeof(struct bcmf_cdc_header), name, name_len);
|
|
|
|
memcpy(frame->data + sizeof(struct bcmf_cdc_header)
|
|
|
|
+ name_len, data, data_len);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_cdc_sendframe(FAR struct bcmf_dev_s *priv, uint32_t cmd,
|
2017-05-04 02:20:57 +02:00
|
|
|
int ifidx, bool set, struct bcmf_frame_s *frame)
|
2017-04-23 22:17:43 +02:00
|
|
|
{
|
2017-05-04 02:20:57 +02:00
|
|
|
struct bcmf_cdc_header *header =
|
|
|
|
(struct bcmf_cdc_header *)frame->data;
|
2017-04-23 22:17:43 +02:00
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
/* Setup control frame header */
|
2017-04-23 22:17:43 +02:00
|
|
|
|
|
|
|
uint32_t cdc_data_len = frame->len - (uint32_t)(frame->data-frame->base);
|
|
|
|
header->cmd = cmd;
|
|
|
|
header->len = cdc_data_len-sizeof(struct bcmf_cdc_header);
|
|
|
|
header->status = 0;
|
2017-04-28 19:28:29 +02:00
|
|
|
header->flags = ++priv->control_reqid << BCMF_CONTROL_REQID_SHIFT;
|
|
|
|
header->flags |= ifidx << BCMF_CONTROL_INTERFACE_SHIFT;
|
2017-04-23 22:17:43 +02:00
|
|
|
|
|
|
|
if (set)
|
|
|
|
{
|
2017-04-28 19:28:29 +02:00
|
|
|
header->flags |= BCMF_CONTROL_SET;
|
2017-04-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
/* Send frame */
|
2017-04-23 22:17:43 +02:00
|
|
|
|
2017-04-30 20:29:48 +02:00
|
|
|
return priv->bus->txframe(priv, frame, true);
|
2017-04-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_cdc_control_request(FAR struct bcmf_dev_s *priv,
|
|
|
|
uint32_t ifidx, bool set, uint32_t cmd,
|
|
|
|
char *name, uint8_t *data, uint32_t *len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Take device control mutex */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
if ((ret = nxsem_wait(&priv->control_mutex)) < 0)
|
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
return ret;
|
2017-10-04 23:22:27 +02:00
|
|
|
}
|
2017-04-23 22:17:43 +02:00
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
ret = bcmf_cdc_control_request_unsafe(priv, ifidx, set, cmd,
|
|
|
|
name, data, len);
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->control_mutex);
|
2017-04-28 19:28:29 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_cdc_control_request_unsafe(FAR struct bcmf_dev_s *priv,
|
|
|
|
uint32_t ifidx, bool set, uint32_t cmd,
|
|
|
|
char *name, uint8_t *data, uint32_t *len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct bcmf_frame_s *frame;
|
|
|
|
uint32_t out_len = *len;
|
|
|
|
|
|
|
|
*len = 0;
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
/* Prepare control frame */
|
|
|
|
|
|
|
|
frame = bcmf_cdc_allocate_frame(priv, name, data, out_len);
|
|
|
|
if (!frame)
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Cannot allocate cdc frame\n");
|
2017-04-28 19:28:29 +02:00
|
|
|
return -ENOMEM;
|
2017-04-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup buffer to store response */
|
|
|
|
|
|
|
|
priv->control_rxdata = set ? NULL : data;
|
|
|
|
priv->control_rxdata_len = out_len;
|
|
|
|
|
|
|
|
/* Send control frame. iovar buffer is freed when sent */
|
|
|
|
|
|
|
|
ret = bcmf_cdc_sendframe(priv, cmd, ifidx, set, frame);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2017-04-30 20:29:48 +02:00
|
|
|
/* Free allocated iovar buffer */
|
|
|
|
|
|
|
|
priv->bus->free_frame(priv, frame);
|
2017-04-28 19:28:29 +02:00
|
|
|
return ret;
|
2017-04-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
ret = bcmf_sem_wait(&priv->control_timeout, BCMF_CONTROL_TIMEOUT_MS);
|
2017-10-05 15:24:54 +02:00
|
|
|
if (ret < 0)
|
2017-04-23 22:17:43 +02:00
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Error while waiting for control response %d\n", ret);
|
2017-04-28 19:28:29 +02:00
|
|
|
return ret;
|
2017-04-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*len = priv->control_rxdata_len;
|
|
|
|
|
|
|
|
/* Check frame status */
|
|
|
|
|
|
|
|
if (priv->control_status != 0)
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Invalid cdc status 0x%x\n", priv->control_status);
|
2017-04-28 19:28:29 +02:00
|
|
|
return -EINVAL;
|
2017-04-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
return OK;
|
2017-04-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int bcmf_cdc_iovar_request(FAR struct bcmf_dev_s *priv,
|
|
|
|
uint32_t ifidx, bool set, char *name,
|
|
|
|
uint8_t *data, uint32_t *len)
|
|
|
|
{
|
|
|
|
return bcmf_cdc_control_request(priv, ifidx, set,
|
|
|
|
set ? WLC_SET_VAR : WLC_GET_VAR, name,
|
|
|
|
data, len);
|
|
|
|
}
|
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
int bcmf_cdc_iovar_request_unsafe(FAR struct bcmf_dev_s *priv,
|
|
|
|
uint32_t ifidx, bool set, char *name,
|
|
|
|
uint8_t *data, uint32_t *len)
|
|
|
|
{
|
|
|
|
return bcmf_cdc_control_request_unsafe(priv, ifidx, set,
|
|
|
|
set ? WLC_SET_VAR : WLC_GET_VAR, name,
|
|
|
|
data, len);
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
int bcmf_cdc_ioctl(FAR struct bcmf_dev_s *priv,
|
|
|
|
uint32_t ifidx, bool set, uint32_t cmd,
|
|
|
|
uint8_t *data, uint32_t *len)
|
|
|
|
{
|
|
|
|
return bcmf_cdc_control_request(priv, ifidx, set, cmd, NULL, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_cdc_process_control_frame(FAR struct bcmf_dev_s *priv,
|
|
|
|
struct bcmf_frame_s *frame)
|
|
|
|
{
|
|
|
|
unsigned int data_size;
|
|
|
|
struct bcmf_cdc_header *cdc_header;
|
|
|
|
|
|
|
|
/* Check frame */
|
|
|
|
|
|
|
|
data_size = frame->len - (unsigned int)(frame->data - frame->base);
|
|
|
|
|
|
|
|
if (data_size < sizeof(struct bcmf_cdc_header))
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Control frame too small\n");
|
2017-04-23 22:17:43 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
cdc_header = (struct bcmf_cdc_header *)frame->data;
|
2017-04-23 22:17:43 +02:00
|
|
|
|
|
|
|
if (data_size < cdc_header->len ||
|
|
|
|
data_size < sizeof(struct bcmf_cdc_header) + cdc_header->len)
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Invalid control frame size\n");
|
2017-04-23 22:17:43 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:35:17 +02:00
|
|
|
/* TODO check interface ? */
|
2017-04-23 22:17:43 +02:00
|
|
|
|
2017-04-28 19:28:29 +02:00
|
|
|
if (cdc_header->flags >> BCMF_CONTROL_REQID_SHIFT == priv->control_reqid)
|
2017-04-23 22:17:43 +02:00
|
|
|
{
|
|
|
|
/* Expected frame received, send it back to user */
|
|
|
|
|
|
|
|
priv->control_status = cdc_header->status;
|
|
|
|
|
|
|
|
if (priv->control_rxdata)
|
|
|
|
{
|
|
|
|
if (priv->control_rxdata_len > cdc_header->len)
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Not enough data %d %d\n",
|
2017-04-23 22:17:43 +02:00
|
|
|
priv->control_rxdata_len, cdc_header->len);
|
|
|
|
priv->control_rxdata_len = cdc_header->len;
|
|
|
|
}
|
2017-05-04 02:20:57 +02:00
|
|
|
|
|
|
|
memcpy(priv->control_rxdata, (uint8_t *)&cdc_header[1],
|
|
|
|
priv->control_rxdata_len);
|
2017-04-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->control_timeout);
|
2017-04-23 22:17:43 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-04-24 00:24:47 +02:00
|
|
|
wlinfo("Got unexpected control frame\n");
|
2017-04-23 22:17:43 +02:00
|
|
|
return -EINVAL;
|
2017-05-04 02:20:57 +02:00
|
|
|
}
|