2017-04-28 19:28:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/wireless/ieee80211/bcmf_bdc.c
|
|
|
|
*
|
2018-04-26 16:10:23 +02:00
|
|
|
* Copyright (C) 2017-2018 Gregory Nutt. All rights reserved.
|
2017-04-28 19:28:29 +02:00
|
|
|
* 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 <string.h>
|
|
|
|
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
|
|
|
|
#include "bcmf_driver.h"
|
|
|
|
#include "bcmf_ioctl.h"
|
|
|
|
#include "bcmf_cdc.h"
|
|
|
|
#include "bcmf_bdc.h"
|
|
|
|
#include "bcmf_utils.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define BCMF_EVENT_ETHER_TYPE 0x6C88 /* Ether type of event frames */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct __attribute__((packed)) bcmf_bdc_header
|
|
|
|
{
|
|
|
|
uint8_t flags; /* bdc frame flags */
|
|
|
|
uint8_t priority; /* bdc frame priority */
|
2018-04-26 16:10:23 +02:00
|
|
|
uint8_t flags2; /* bdc frame additional flags */
|
|
|
|
uint8_t data_offset; /* Offset from end of header to payload data, in 4-bytes count */
|
2017-04-28 19:28:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct __attribute__((packed)) bcmf_eth_header
|
|
|
|
{
|
|
|
|
uint16_t type; /* Vendor specific type */
|
|
|
|
uint16_t len; /* Event data length */
|
|
|
|
uint8_t version; /* Protocol version */
|
|
|
|
uint8_t oui[3]; /* Organizationally unique identifier */
|
|
|
|
uint16_t usr_type; /* User specific type */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct __attribute__((packed)) bcmf_event_msg
|
|
|
|
{
|
|
|
|
struct ether_header eth;
|
|
|
|
struct bcmf_eth_header bcm_eth;
|
|
|
|
struct bcmf_event_s event;
|
|
|
|
uint8_t data[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-23 01:35:17 +02:00
|
|
|
static const uint8_t bcmf_broadcom_oui[] =
|
|
|
|
{
|
|
|
|
0x00, 0x10, 0x18
|
|
|
|
};
|
2017-04-28 19:28:29 +02:00
|
|
|
|
2017-04-30 20:29:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
struct bcmf_frame_s *bcmf_bdc_allocate_frame(FAR struct bcmf_dev_s *priv,
|
|
|
|
uint32_t len, bool block)
|
2017-04-28 19:28:29 +02:00
|
|
|
{
|
2017-04-30 20:29:48 +02:00
|
|
|
struct bcmf_frame_s *frame;
|
2017-04-28 19:28:29 +02:00
|
|
|
|
|
|
|
/* Allocate data frame */
|
2018-08-23 01:35:17 +02:00
|
|
|
/* TODO check for integer overflow */
|
2017-04-28 19:28:29 +02:00
|
|
|
|
2017-04-30 20:29:48 +02:00
|
|
|
frame = priv->bus->allocate_frame(priv,
|
|
|
|
sizeof(struct bcmf_bdc_header) + len, block, false);
|
2017-04-28 19:28:29 +02:00
|
|
|
|
2017-04-30 20:29:48 +02:00
|
|
|
if (!frame)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-28 19:28:29 +02:00
|
|
|
|
2017-04-30 20:29:48 +02:00
|
|
|
frame->data += sizeof(struct bcmf_bdc_header);
|
|
|
|
|
2017-05-04 02:30:40 +02:00
|
|
|
return frame;
|
2017-04-28 19:28:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_bdc_process_event_frame(FAR struct bcmf_dev_s *priv,
|
|
|
|
struct bcmf_frame_s *frame)
|
|
|
|
{
|
|
|
|
int data_size;
|
|
|
|
struct bcmf_bdc_header *header;
|
|
|
|
struct bcmf_event_msg *event_msg;
|
|
|
|
uint32_t event_id;
|
|
|
|
event_handler_t handler;
|
|
|
|
|
|
|
|
/* Check frame header */
|
|
|
|
|
|
|
|
data_size = frame->len - (int)(frame->data - frame->base);
|
|
|
|
|
|
|
|
if (data_size < sizeof(struct bcmf_bdc_header))
|
|
|
|
{
|
|
|
|
goto exit_invalid_frame;
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
header = (struct bcmf_bdc_header *)frame->data;
|
2017-04-28 19:28:29 +02:00
|
|
|
|
2018-04-26 16:10:23 +02:00
|
|
|
data_size -= sizeof(struct bcmf_bdc_header) + header->data_offset * 4;
|
2017-04-28 19:28:29 +02:00
|
|
|
|
|
|
|
if (data_size < sizeof(struct bcmf_event_msg))
|
|
|
|
{
|
|
|
|
goto exit_invalid_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_size -= sizeof(struct ether_header) + sizeof(struct bcmf_eth_header);
|
|
|
|
|
|
|
|
/* Check ethernet header */
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
event_msg = (struct bcmf_event_msg *)(frame->data +
|
|
|
|
sizeof(struct bcmf_bdc_header) +
|
2018-04-26 16:10:23 +02:00
|
|
|
header->data_offset * 4);
|
2017-04-28 19:28:29 +02:00
|
|
|
|
|
|
|
if (event_msg->eth.ether_type != BCMF_EVENT_ETHER_TYPE ||
|
|
|
|
memcmp(event_msg->bcm_eth.oui, bcmf_broadcom_oui, 3))
|
|
|
|
{
|
|
|
|
goto exit_invalid_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_id = bcmf_getle32(&event_msg->event.type);
|
|
|
|
|
|
|
|
if (event_id >= BCMF_EVENT_COUNT)
|
|
|
|
{
|
|
|
|
wlinfo("Invalid event id %d\n", event_id);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispatch event to registered handler */
|
|
|
|
|
|
|
|
handler = priv->event_handlers[event_id];
|
|
|
|
if (handler != NULL)
|
|
|
|
{
|
|
|
|
handler(priv, &event_msg->event, data_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
exit_invalid_frame:
|
|
|
|
wlerr("Invalid event frame\n");
|
|
|
|
bcmf_hexdump(frame->base, frame->len, (unsigned long)frame->base);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_event_register(FAR struct bcmf_dev_s *priv, event_handler_t handler,
|
|
|
|
unsigned int event_id)
|
|
|
|
{
|
|
|
|
if (event_id >= BCMF_EVENT_COUNT)
|
|
|
|
{
|
|
|
|
/* Invalid event id */
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->event_handlers[event_id] = handler;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_event_unregister(FAR struct bcmf_dev_s *priv,
|
|
|
|
unsigned int event_id)
|
|
|
|
{
|
|
|
|
return bcmf_event_register(priv, NULL, event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_event_push_config(FAR struct bcmf_dev_s *priv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint32_t out_len;
|
2017-05-04 02:20:57 +02:00
|
|
|
uint8_t event_mask[(BCMF_EVENT_COUNT + 7) >> 3];
|
2017-04-28 19:28:29 +02:00
|
|
|
|
|
|
|
memset(event_mask, 0, sizeof(event_mask));
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
for (i = 0; i < BCMF_EVENT_COUNT; i++)
|
2017-04-28 19:28:29 +02:00
|
|
|
{
|
|
|
|
if (priv->event_handlers[i] != NULL)
|
|
|
|
{
|
2017-05-04 02:20:57 +02:00
|
|
|
event_mask[i >> 3] |= 1 << (i & 0x7);
|
2017-04-28 19:28:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send event mask to chip */
|
|
|
|
|
|
|
|
out_len = sizeof(event_mask);
|
|
|
|
if (bcmf_cdc_iovar_request(priv, CHIP_STA_INTERFACE, true,
|
|
|
|
IOVAR_STR_EVENT_MSGS, event_mask,
|
|
|
|
&out_len))
|
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2017-04-30 20:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int bcmf_bdc_transmit_frame(FAR struct bcmf_dev_s *priv,
|
2017-05-04 02:20:57 +02:00
|
|
|
struct bcmf_frame_s *frame)
|
2017-04-30 20:29:48 +02:00
|
|
|
{
|
2017-05-04 02:20:57 +02:00
|
|
|
struct bcmf_bdc_header *header;
|
2017-04-30 20:29:48 +02:00
|
|
|
|
|
|
|
/* Set frame data for lower layer */
|
|
|
|
|
|
|
|
frame->data -= sizeof(struct bcmf_bdc_header);
|
2017-05-04 02:20:57 +02:00
|
|
|
header = (struct bcmf_bdc_header *)frame->data;
|
2017-04-30 20:29:48 +02:00
|
|
|
|
|
|
|
/* Setup data frame header */
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
header->flags = 0x20; /* Set bdc protocol version */
|
2018-08-23 01:35:17 +02:00
|
|
|
header->priority = 0; /* TODO handle priority */
|
2017-05-04 02:20:57 +02:00
|
|
|
header->flags2 = CHIP_STA_INTERFACE;
|
2017-04-30 20:29:48 +02:00
|
|
|
header->data_offset = 0;
|
|
|
|
|
|
|
|
/* Send frame */
|
|
|
|
|
|
|
|
return priv->bus->txframe(priv, frame, false);
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:20:57 +02:00
|
|
|
struct bcmf_frame_s *bcmf_bdc_rx_frame(FAR struct bcmf_dev_s *priv)
|
2017-04-30 20:29:48 +02:00
|
|
|
{
|
|
|
|
unsigned int frame_len;
|
|
|
|
struct bcmf_frame_s *frame = priv->bus->rxframe(priv);
|
2018-04-26 16:10:23 +02:00
|
|
|
struct bcmf_bdc_header *header;
|
2017-04-30 20:29:48 +02:00
|
|
|
|
2018-08-23 01:02:23 +02:00
|
|
|
/* Very that there is an Rx frame */
|
|
|
|
|
|
|
|
if (frame == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-30 20:29:48 +02:00
|
|
|
/* Process bdc header */
|
|
|
|
|
|
|
|
frame_len = frame->len - (unsigned int)(frame->data - frame->base);
|
|
|
|
|
|
|
|
if (frame_len < sizeof(struct bcmf_bdc_header))
|
|
|
|
{
|
|
|
|
wlerr("Data frame too small\n");
|
|
|
|
priv->bus->free_frame(priv, frame);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transmit frame to upper layer */
|
|
|
|
|
2018-08-23 01:35:17 +02:00
|
|
|
header = (struct bcmf_bdc_header *)frame->data;
|
2018-04-26 16:10:23 +02:00
|
|
|
frame->data += sizeof(struct bcmf_bdc_header) + header->data_offset * 4;
|
2017-04-30 20:29:48 +02:00
|
|
|
return frame;
|
2017-05-04 02:20:57 +02:00
|
|
|
}
|