2017-04-13 20:31:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/wireless/ieee80211/bcmf_sdpcm.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>
|
2017-04-16 11:28:08 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <queue.h>
|
|
|
|
#include <semaphore.h>
|
2017-04-13 20:31:39 +02:00
|
|
|
|
|
|
|
#include "bcmf_sdio.h"
|
|
|
|
#include "bcmf_core.h"
|
|
|
|
#include "bcmf_sdpcm.h"
|
2017-04-23 22:17:43 +02:00
|
|
|
#include "bcmf_cdc.h"
|
|
|
|
#include "bcmf_utils.h"
|
|
|
|
|
|
|
|
#include "bcmf_sdio_regs.h"
|
2017-04-16 11:28:08 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2017-04-13 20:31:39 +02:00
|
|
|
|
|
|
|
/* SDA_FRAMECTRL */
|
|
|
|
#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
|
|
|
|
#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
|
|
|
|
#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
|
|
|
|
#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
|
|
|
|
|
|
|
|
/* tosbmailbox bits corresponding to intstatus bits */
|
2017-04-16 11:28:08 +02:00
|
|
|
#define SMB_NAK (1 << 0) /* Frame NAK */
|
2017-04-13 20:31:39 +02:00
|
|
|
#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
|
|
|
|
#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
|
|
|
|
#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
|
|
|
|
|
2017-04-16 11:28:08 +02:00
|
|
|
#define SDPCM_CONTROL_CHANNEL 0 /* Control */
|
|
|
|
#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
|
|
|
|
#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
|
|
|
|
#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
|
|
|
|
#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
#define container_of(ptr, type, member) \
|
|
|
|
(type *)( (uint8_t *)(ptr) - offsetof(type,member) )
|
2017-04-13 20:31:39 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-04-16 11:28:08 +02:00
|
|
|
struct bcmf_sdpcm_header {
|
|
|
|
uint16_t size;
|
|
|
|
uint16_t checksum;
|
|
|
|
uint8_t sequence;
|
|
|
|
uint8_t channel;
|
|
|
|
uint8_t next_length;
|
|
|
|
uint8_t data_offset;
|
|
|
|
uint8_t flow_control;
|
|
|
|
uint8_t credit;
|
|
|
|
uint16_t padding;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bcmf_sdpcm_frame {
|
2017-04-23 22:17:43 +02:00
|
|
|
struct bcmf_frame_s frame_header;
|
2017-04-16 11:28:08 +02:00
|
|
|
dq_entry_t list_entry;
|
|
|
|
};
|
|
|
|
|
2017-04-13 20:31:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
static int bcmf_sdpcm_rxfail(FAR struct bcmf_sdio_dev_s *sbus, bool retry);
|
2017-04-13 20:31:39 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
static int bcmf_sdpcm_process_header(FAR struct bcmf_sdio_dev_s *sbus,
|
2017-04-16 11:28:08 +02:00
|
|
|
struct bcmf_sdpcm_header *header);
|
|
|
|
|
2017-04-13 20:31:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
int bcmf_sdpcm_rxfail(FAR struct bcmf_sdio_dev_s *sbus, bool retry)
|
2017-04-13 20:31:39 +02:00
|
|
|
{
|
|
|
|
/* issue abort command for F2 through F0 */
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
bcmf_write_reg(sbus, 0, SDIO_CCCR_IOABORT, 2);
|
2017-04-13 20:31:39 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
bcmf_write_reg(sbus, 1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM);
|
2017-04-13 20:31:39 +02:00
|
|
|
|
|
|
|
/* TODO Wait until the packet has been flushed (device/FIFO stable) */
|
|
|
|
|
|
|
|
/* Send NAK to retry to read frame */
|
|
|
|
if (retry)
|
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
bcmf_write_sbregb(sbus,
|
|
|
|
CORE_BUS_REG(sbus->chip->core_base[SDIOD_CORE_ID],
|
2017-04-13 20:31:39 +02:00
|
|
|
tosbmailbox), SMB_NAK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
int bcmf_sdpcm_process_header(FAR struct bcmf_sdio_dev_s *sbus,
|
2017-04-13 20:31:39 +02:00
|
|
|
struct bcmf_sdpcm_header *header)
|
|
|
|
{
|
|
|
|
if (header->data_offset < sizeof(struct bcmf_sdpcm_header) ||
|
2017-04-16 11:28:08 +02:00
|
|
|
header->data_offset > header->size)
|
2017-04-13 20:31:39 +02:00
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Invalid data offset\n");
|
2017-04-23 22:17:43 +02:00
|
|
|
bcmf_sdpcm_rxfail(sbus, false);
|
2017-04-13 20:31:39 +02:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update tx credits */
|
|
|
|
|
2017-04-24 00:24:47 +02:00
|
|
|
// wlinfo("update credit %x %x %x\n", header->credit,
|
2017-04-23 22:17:43 +02:00
|
|
|
// sbus->tx_seq, sbus->max_seq);
|
2017-04-13 20:31:39 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (header->credit - sbus->tx_seq > 0x40)
|
2017-04-13 20:31:39 +02:00
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("seq %d: max tx seq number error\n", sbus->tx_seq);
|
2017-04-23 22:17:43 +02:00
|
|
|
sbus->max_seq = sbus->tx_seq + 2;
|
2017-04-13 20:31:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
sbus->max_seq = header->credit;
|
2017-04-13 20:31:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-04-16 11:28:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-04-13 20:31:39 +02:00
|
|
|
// FIXME remove
|
2017-04-22 15:57:51 +02:00
|
|
|
uint8_t tmp_buffer[1024];
|
2017-04-13 20:31:39 +02:00
|
|
|
int bcmf_sdpcm_readframe(FAR struct bcmf_dev_s *priv)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint16_t len, checksum;
|
2017-04-23 22:17:43 +02:00
|
|
|
struct bcmf_sdpcm_header *header;
|
|
|
|
struct bcmf_sdpcm_frame *sframe;
|
|
|
|
FAR struct bcmf_sdio_dev_s *sbus = (FAR struct bcmf_sdio_dev_s*)priv->bus;
|
|
|
|
|
|
|
|
/* TODO request free frame buffer */
|
|
|
|
|
|
|
|
sframe = (struct bcmf_sdpcm_frame*)tmp_buffer;
|
|
|
|
header = (struct bcmf_sdpcm_header*)&sframe[1];
|
2017-04-13 20:31:39 +02:00
|
|
|
|
|
|
|
/* Read header */
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
ret = bcmf_transfer_bytes(sbus, false, 2, 0, (uint8_t*)header, 4);
|
2017-04-13 20:31:39 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlinfo("failread size\n");
|
2017-04-13 20:31:39 +02:00
|
|
|
ret = -EIO;
|
|
|
|
goto exit_abort;
|
|
|
|
}
|
|
|
|
|
2017-04-16 11:28:08 +02:00
|
|
|
len = header->size;
|
|
|
|
checksum = header->checksum;
|
2017-04-13 20:31:39 +02:00
|
|
|
|
|
|
|
/* All zero means no more to read */
|
|
|
|
|
|
|
|
if (!(len | checksum))
|
|
|
|
{
|
|
|
|
return -ENODATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((~len & 0xffff) ^ checksum) || len < sizeof(struct bcmf_sdpcm_header))
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Invalid header checksum or len %x %x\n", len, checksum);
|
2017-04-16 13:13:11 +02:00
|
|
|
ret = -EINVAL;
|
2017-04-13 20:31:39 +02:00
|
|
|
goto exit_abort;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME define for size
|
2017-04-22 15:57:51 +02:00
|
|
|
if (len > sizeof(tmp_buffer))
|
2017-04-13 20:31:39 +02:00
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Frame is too large, cancel %d\n", len);
|
2017-04-13 20:31:39 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto exit_abort;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read remaining frame data */
|
2017-04-16 13:13:11 +02:00
|
|
|
// TODO allocate buffer
|
2017-04-13 20:31:39 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
ret = bcmf_transfer_bytes(sbus, false, 2, 0, (uint8_t*)header+4, len - 4);
|
2017-04-13 20:31:39 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
2017-04-16 13:13:11 +02:00
|
|
|
goto exit_free_abort;
|
2017-04-13 20:31:39 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 00:24:47 +02:00
|
|
|
// wlinfo("Receive frame\n");
|
2017-04-23 22:17:43 +02:00
|
|
|
// bcmf_hexdump((uint8_t*)header, header->size, (unsigned int)header);
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-13 20:31:39 +02:00
|
|
|
/* Process and validate header */
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
ret = bcmf_sdpcm_process_header(sbus, header);
|
2017-04-13 20:31:39 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Error while processing header %d\n", ret);
|
2017-04-16 13:13:11 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto exit_free_frame;
|
2017-04-13 20:31:39 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
/* Setup new frame structure */
|
|
|
|
|
|
|
|
sframe->frame_header.len = header->size;
|
|
|
|
sframe->frame_header.data = (uint8_t*)header + header->data_offset;
|
|
|
|
sframe->frame_header.base = (uint8_t*)header;
|
|
|
|
|
2017-04-16 13:13:11 +02:00
|
|
|
/* Process received frame content */
|
|
|
|
|
|
|
|
switch (header->channel & 0x0f)
|
|
|
|
{
|
|
|
|
case SDPCM_CONTROL_CHANNEL:
|
2017-04-23 22:17:43 +02:00
|
|
|
ret = bcmf_cdc_process_control_frame(priv, &sframe->frame_header);
|
2017-04-16 13:13:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDPCM_EVENT_CHANNEL:
|
2017-04-23 22:17:43 +02:00
|
|
|
ret = bcmf_cdc_process_event_frame(priv, &sframe->frame_header);
|
2017-04-16 13:13:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDPCM_DATA_CHANNEL:
|
2017-04-23 22:17:43 +02:00
|
|
|
ret = bcmf_cdc_process_data_frame(priv, &sframe->frame_header);
|
2017-04-16 13:13:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("Got unexpected message type %d\n", header->channel);
|
2017-04-16 13:13:11 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit_free_frame:
|
|
|
|
// TODO free frame buffer
|
2017-04-13 20:31:39 +02:00
|
|
|
return ret;
|
|
|
|
|
2017-04-16 13:13:11 +02:00
|
|
|
exit_free_abort:
|
|
|
|
// TODO free frame buffer
|
2017-04-13 20:31:39 +02:00
|
|
|
exit_abort:
|
2017-04-23 22:17:43 +02:00
|
|
|
bcmf_sdpcm_rxfail(sbus, false);
|
2017-04-13 20:31:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-16 11:28:08 +02:00
|
|
|
int bcmf_sdpcm_sendframe(FAR struct bcmf_dev_s *priv)
|
2017-04-13 20:31:39 +02:00
|
|
|
{
|
2017-04-16 11:28:08 +02:00
|
|
|
int ret;
|
2017-04-23 22:17:43 +02:00
|
|
|
struct bcmf_sdpcm_frame *sframe;
|
|
|
|
struct bcmf_sdpcm_header *header;
|
|
|
|
FAR struct bcmf_sdio_dev_s *sbus = (FAR struct bcmf_sdio_dev_s*)priv->bus;
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (sbus->tx_queue.tail == NULL)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
|
|
|
/* No more frames to send */
|
|
|
|
|
|
|
|
return -ENODATA;
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (sbus->tx_seq == sbus->max_seq)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
|
|
|
// TODO handle this case
|
2017-04-24 00:24:47 +02:00
|
|
|
wlerr("No credit to send frame\n");
|
2017-04-16 11:28:08 +02:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if ((ret = sem_wait(&sbus->tx_queue_mutex)) != OK)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
sframe = container_of(sbus->tx_queue.tail,
|
|
|
|
struct bcmf_sdpcm_frame, list_entry);
|
|
|
|
header = (struct bcmf_sdpcm_header*)sframe->frame_header.base;
|
2017-04-16 11:28:08 +02:00
|
|
|
|
|
|
|
/* Set frame sequence id */
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
header->sequence = sbus->tx_seq++;
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-24 00:24:47 +02:00
|
|
|
// wlinfo("Send frame\n");
|
2017-04-23 22:17:43 +02:00
|
|
|
// bcmf_hexdump(sframe->frame_header.base, sframe->frame_header.len,
|
|
|
|
// (unsigned long)sframe->frame_header.base);
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
ret = bcmf_transfer_bytes(sbus, true, 2, 0, sframe->frame_header.base,
|
|
|
|
sframe->frame_header.len);
|
2017-04-16 11:28:08 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2017-04-24 00:24:47 +02:00
|
|
|
wlinfo("fail send frame %d\n", ret);
|
2017-04-16 11:28:08 +02:00
|
|
|
ret = -EIO;
|
|
|
|
goto exit_abort;
|
|
|
|
// TODO handle retry count and remove frame from queue + abort TX
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Frame sent, remove it from queue */
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (sbus->tx_queue.head == &sframe->list_entry)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
|
|
|
/* List is empty */
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
sbus->tx_queue.head = NULL;
|
|
|
|
sbus->tx_queue.tail = NULL;
|
2017-04-16 11:28:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
sbus->tx_queue.tail = sframe->list_entry.blink;
|
|
|
|
sframe->list_entry.blink->flink = sbus->tx_queue.head;
|
2017-04-16 11:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO free frame buffer */
|
|
|
|
|
|
|
|
goto exit_post_sem;
|
|
|
|
|
|
|
|
exit_abort:
|
2017-04-23 22:17:43 +02:00
|
|
|
// bcmf_sdpcm_txfail(sbus, false);
|
2017-04-16 11:28:08 +02:00
|
|
|
exit_post_sem:
|
2017-04-23 22:17:43 +02:00
|
|
|
sem_post(&sbus->tx_queue_mutex);
|
2017-04-16 11:28:08 +02:00
|
|
|
return ret;
|
2017-04-13 20:31:39 +02:00
|
|
|
}
|
2017-04-16 11:28:08 +02:00
|
|
|
|
|
|
|
// FIXME remove
|
|
|
|
uint8_t tmp_buffer2[512];
|
2017-04-23 22:17:43 +02:00
|
|
|
struct bcmf_frame_s* bcmf_sdpcm_allocate_frame(FAR struct bcmf_dev_s *priv,
|
|
|
|
unsigned int len, bool control, bool block)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
unsigned int frame_len;
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
/* Integer overflow check */
|
2017-04-22 15:57:51 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (len > 512)
|
2017-04-22 15:57:51 +02:00
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
return NULL;
|
2017-04-22 15:57:51 +02:00
|
|
|
}
|
2017-04-23 22:17:43 +02:00
|
|
|
|
|
|
|
frame_len = len + sizeof(struct bcmf_sdpcm_frame)
|
|
|
|
+ sizeof(struct bcmf_sdpcm_header);
|
|
|
|
if (!control)
|
2017-04-22 15:57:51 +02:00
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
/* Data frames needs 2 bytes padding */
|
2017-04-22 15:57:51 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
frame_len += 2;
|
|
|
|
}
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (frame_len > 512)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
// FIXME allocate buffer and use max_size instead of 512
|
|
|
|
// allocate buffer len + sizeof(struct bcmf_sdpcm_frame)
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
struct bcmf_sdpcm_frame *sframe = (struct bcmf_sdpcm_frame*)tmp_buffer2;
|
|
|
|
struct bcmf_sdpcm_header *header = (struct bcmf_sdpcm_header*)&sframe[1];
|
2017-04-16 11:28:08 +02:00
|
|
|
|
|
|
|
/* Prepare sw header */
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
memset(header, 0, sizeof(struct bcmf_sdpcm_header));
|
|
|
|
header->size = frame_len - sizeof(struct bcmf_sdpcm_frame);
|
|
|
|
header->checksum = ~header->size;
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (control)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
header->channel = SDPCM_CONTROL_CHANNEL;
|
|
|
|
header->data_offset = sizeof(struct bcmf_sdpcm_header);
|
2017-04-16 11:28:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
header->channel = SDPCM_DATA_CHANNEL;
|
|
|
|
header->data_offset = sizeof(struct bcmf_sdpcm_header)+2;
|
2017-04-16 11:28:08 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
sframe->frame_header.len = header->size;
|
|
|
|
sframe->frame_header.base = (uint8_t*)header;
|
|
|
|
sframe->frame_header.data = (uint8_t*)header + header->data_offset;
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
return &sframe->frame_header;
|
2017-04-16 11:28:08 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
int bcmf_sdpcm_queue_frame(FAR struct bcmf_dev_s *priv,
|
|
|
|
struct bcmf_frame_s *frame)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2017-04-23 22:17:43 +02:00
|
|
|
FAR struct bcmf_sdio_dev_s *sbus = (FAR struct bcmf_sdio_dev_s*)priv->bus;
|
|
|
|
struct bcmf_sdpcm_frame *sframe = (struct bcmf_sdpcm_frame*)frame;
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
/* Add frame in tx queue */
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if ((ret = sem_wait(&sbus->tx_queue_mutex)) != OK)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
return ret;
|
2017-04-16 11:28:08 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
if (sbus->tx_queue.head == NULL)
|
2017-04-16 11:28:08 +02:00
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
/* List is empty */
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
sbus->tx_queue.head = &sframe->list_entry;
|
|
|
|
sbus->tx_queue.tail = &sframe->list_entry;
|
2017-04-16 13:13:11 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
sframe->list_entry.flink = &sframe->list_entry;
|
|
|
|
sframe->list_entry.blink = &sframe->list_entry;
|
2017-04-16 11:28:08 +02:00
|
|
|
}
|
2017-04-23 22:17:43 +02:00
|
|
|
else
|
2017-04-22 15:57:51 +02:00
|
|
|
{
|
2017-04-23 22:17:43 +02:00
|
|
|
/* Insert entry at list head */
|
2017-04-16 11:28:08 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
sframe->list_entry.flink = sbus->tx_queue.head;
|
|
|
|
sframe->list_entry.blink = sbus->tx_queue.tail;
|
|
|
|
|
|
|
|
sbus->tx_queue.head->blink = &sframe->list_entry;
|
|
|
|
sbus->tx_queue.head = &sframe->list_entry;
|
2017-04-22 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
sem_post(&sbus->tx_queue_mutex);
|
2017-04-16 13:13:11 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
/* Notify bcmf thread tx frame is ready */
|
2017-04-22 15:57:51 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
sem_post(&sbus->thread_signal);
|
2017-04-22 15:57:51 +02:00
|
|
|
|
2017-04-23 22:17:43 +02:00
|
|
|
return OK;
|
2017-04-16 11:28:08 +02:00
|
|
|
}
|