/*
* Shared Transport Line discipline driver Core
* This hooks up ST KIM driver and ST LL driver
* Copyright (C) 2009-2010 Texas Instruments
* Author: Pavan Savoy <pavan_savoy@ti.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define pr_fmt(fmt) "(stc): " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/tty.h>
/* understand BT, FM and GPS for now */
#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>
#include <net/bluetooth/hci.h>
#include <linux/ti_wilink_st.h>
/* function pointer pointing to either,
* st_kim_recv during registration to receive fw download responses
* st_int_recv after registration to receive proto stack responses
*/
void (*st_recv) (void*, const unsigned char*, long);
/********************************************************************/
#if 0
/* internal misc functions */
bool is_protocol_list_empty(void)
{
unsigned char i = 0;
pr_debug(" %s ", __func__);
for (i = 0; i < ST_MAX; i++) {
if (st_gdata->list[i] != NULL)
return ST_NOTEMPTY;
/* not empty */
}
/* list empty */
return ST_EMPTY;
}
#endif
/* can be called in from
* -- KIM (during fw download)
* -- ST Core (during st_write)
*
* This is the internal write function - a wrapper
* to tty->ops->write
*/
int st_int_write(struct st_data_s *st_gdata,
const unsigned char *data, int count)
{
struct tty_struct *tty;
if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
pr_err("tty unavailable to perform write");
return -1;
}
tty = st_gdata->tty;
#ifdef VERBOSE
print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
16, 1, data, count, 0);
#endif
return tty->ops->write(tty, data, count);
}
/*
* push the skb received to relevant
* protocol stacks
*/
void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
{
pr_info(" %s(prot:%d) ", __func__, protoid);
if (unlikely
(st_gdata == NULL || st_gdata->rx_skb == NULL
|| st_gdata->list[protoid] == NULL)) {
pr_err("protocol %d not registered, no data to send?",
protoid);
kfree_skb(st_gdata->rx_skb);
return;
}
/* this cannot fail
* this shouldn't take long
* - should be just skb_queue_tail for the
* protocol stack driver
*/
if (likely(st_gdata->list[protoid]->recv != NULL)) {
if (unlikely
(st_gdata->list[protoid]->recv
(st_gdata->list[protoid]->priv_data, st_gdata->rx_skb)
!= 0)) {
pr_err(" proto stack %d's ->recv failed", protoid);
kfree_skb(st_gdata->rx_skb);
return;
}
} else {
pr_err(" proto stack %d's ->recv null", protoid);
kfree_skb(st_gdata->rx_skb);
}
return;
}
/**
* st_reg_complete -
* to call registration complete callbacks
* of all protocol stack drivers
*/
void st_reg_complete(struct st_data_s *st_gdata, char err)
{
unsigned char i = 0;
pr_info(" %s ", __func__);
for (i = 0; i < ST_MAX; i++) {
if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
st_gdata->list[i]->reg_complete_cb != NULL))
st_gdata->list[i]->reg_complete_cb
(st_gdata->list[i]->priv_data, err);
}
}
static inline int st_check_data_len(struct st_data_s *st_gdata,
int protoid, int len)
{
int room = skb_tailroom(st_gdata->rx_skb);
pr_debug("len %d room %d", len, room);
if (!len) {
/* Received packet has only packet header and
* has zero length payload. So, ask ST CORE to
* forward the packet to protocol driver (BT/FM/GPS)
*/
st_send_frame(protoid, st_gdata);
} else if (len > room) {
/* Received packet's payload length is larger.
* We can't accommodate it in created skb.
*/
pr_err("Data length is too large len %d room %d", len,
room);
kfree_skb(st_gdata->rx_skb);
} else {
/* Packet header has non-zero payload length and
* we have enough space in created skb. Lets read
* payload data */
st_gdata->rx_state = ST_BT_W4_DATA;
st_gdata->rx_count = len;
return len;
}
/* Change ST state to continue to process next
* packet */
st_gdata->rx_state = ST_W4_PACKET_TYPE;
st_gdata->rx_skb = NULL;
st_gdata->rx_count = 0;
return 0;
}
/**
* st_wakeup_ack - internal function for action when wake-up ack
* received
*/
static inline void st_wakeup_ack(struct st_data_s *st_gdata,
unsigned char cmd)
{
struct sk_buff *waiting_skb;
unsigned lo