rsi_91x_hci.c 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/*
 * Copyright (c) 2017 Redpine Signals Inc. All rights reserved.
 *
 * 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 of the copyright holder 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 HOLDER 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.
 */

#include "rsi_hci.h"
#include "rsi_mgmt.h"
#include "rsi_coex.h"
#include "rsi_hal.h"

/**
 * rsi_hci_open() - This function is called when HCI device is
 * 						opened 
 * 
 * @hdev - pointer to HCI device
 * @return - 0 on success
 */
static int rsi_hci_open(struct hci_dev *hdev)
{
	rsi_dbg(ERR_ZONE, "RSI HCI DEVICE \"%s\" open\n", hdev->name);

	if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
		rsi_dbg(ERR_ZONE, "%s: device `%s' already running\n", 
				__func__, hdev->name);

	return 0;
}

/**
 * rsi_hci_close() - This function is called when HCI device is
 * 						closed 
 * 
 * @hdev - pointer to HCI device
 * @return - 0 on success
 */
static int rsi_hci_close(struct hci_dev *hdev)
{
	rsi_dbg(ERR_ZONE, "RSI HCI DEVICE \"%s\" closed\n", hdev->name);

	if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
		rsi_dbg(ERR_ZONE, "%s: device `%s' not running\n",
				 __func__, hdev->name);

	return 0;
}

/**
 * rsi_hci_flush() - This function is called when HCI device is
 * 						flushed 
 * 
 * @hdev - pointer to HCI device
 * @return - 0 on success; negative error code on failure
 */
static int rsi_hci_flush(struct hci_dev *hdev)
{
	struct rsi_hci_adapter *h_adapter;

	if (!(h_adapter = hci_get_drvdata(hdev)))
		return -EFAULT;

	rsi_dbg(ERR_ZONE, "RSI `%s' flush\n", hdev->name);

	return 0;
}

/**
 * rsi_hci_send_pkt() - This function is used send the packet received 
 * 						from HCI layer to co-ex module
 *
 * @hdev - pointer to HCI device
 * @skb - Received packet from HCI
 * @return - 0 on success; negative error code on failure
 *  
 */
#if LINUX_VERSION_CODE < KERNEL_VERSION (3, 13, 0)
static int rsi_hci_send_pkt(struct sk_buff *skb)
#else
static int rsi_hci_send_pkt(struct hci_dev *hdev, struct sk_buff *skb)
#endif
{
	struct rsi_hci_adapter *h_adapter;
	struct sk_buff *new_skb = NULL;
#if LINUX_VERSION_CODE < KERNEL_VERSION (3, 13, 0)
	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
#endif
	int status = 0;

	if (skb->len <= 0) {
		rsi_dbg(ERR_ZONE, "Zero length packet\n");
		//hdev->sta.err_tx++;
		status = -EINVAL;
		goto fail;
	}

	if (!(h_adapter = hci_get_drvdata(hdev))) {
		//hdev->sta.err_tx++;
		status = -EFAULT;
		goto fail;
	}

#ifdef CONFIG_RSI_WOW
	/* Stop here when in suspend */
	if (h_adapter->priv->wow_flags & RSI_WOW_ENABLED) {
		rsi_dbg(INFO_ZONE, "In suspend: Dropping the pkt\n");
		status = -ENETDOWN;
		goto fail;
	}
#endif

	if (h_adapter->priv->bt_fsm_state != BT_DEVICE_READY) {
		rsi_dbg(ERR_ZONE, "BT Device not ready\n");
		status = -ENODEV;
		goto fail;
	}

	if (!test_bit(HCI_RUNNING, &hdev->flags)) {
		status = -EBUSY;
		goto fail;
	}

	switch (bt_cb(skb)->pkt_type) {
		case HCI_COMMAND_PKT:
			hdev->stat.cmd_tx++;
			break;

		case HCI_ACLDATA_PKT:
			hdev->stat.acl_tx++;
			break;

		case HCI_SCODATA_PKT:
			hdev->stat.sco_tx++;
			break;

		default:
			dev_kfree_skb(skb);
			status = -EILSEQ;
			goto fail;
	}

	if (skb_headroom(skb) < REQUIRED_HEADROOM_FOR_BT_HAL) {
		/* Re-allocate one more skb with sufficent headroom 
		 * make copy of input-skb to new one */
		new_skb = skb_realloc_headroom(skb, REQUIRED_HEADROOM_FOR_BT_HAL);
		if(unlikely(!new_skb))
			return -ENOMEM;
#if LINUX_VERSION_CODE >= KERNEL_VERSION (4, 5, 0)
		bt_cb(new_skb)->pkt_type = hci_skb_pkt_type(skb);
#else
		bt_cb(new_skb)->pkt_type = bt_cb((skb))->pkt_type;
#endif
		kfree_skb(skb);
		skb = new_skb;
		if (!IS_ALIGNED((unsigned long)skb->data, RSI_DMA_ALIGN))
			skb = rsi_get_aligned_skb(skb);
	}

        rsi_hex_dump(DATA_RX_ZONE, "TX BT Pkt", skb->data, skb->len); 

#ifdef CONFIG_RSI_COEX_MODE
	rsi_coex_send_pkt(h_adapter->priv, skb, BT_Q);
#else
        rsi_send_bt_pkt(h_adapter->priv, skb);
#endif
	return 0;

fail:
	dev_kfree_skb(skb);
	return status;
}

int rsi_send_rfmode_frame(struct rsi_common *common)
{
	struct sk_buff *skb;
	struct rsi_bt_rfmode_frame *cmd_frame;
	int status;

	rsi_dbg(MGMT_TX_ZONE, "%s: Sending BT RF mode frame\n", __func__);

	skb = dev_alloc_skb(sizeof(struct rsi_bt_rfmode_frame));
	if (!skb)
		return -ENOMEM;

	memset(skb->data, 0, sizeof(struct rsi_bt_rfmode_frame));
	cmd_frame = (struct rsi_bt_rfmode_frame *)skb->data;

	/* Length is 0 */
	cmd_frame->desc.q_no = RSI_BT_MGMT_Q;
	cmd_frame->desc.pkt_type = RSI_BT_PKT_TYPE_RFMODE;
	cmd_frame->bt_rf_tx_power_mode = 0;
	cmd_frame->bt_rf_tx_power_mode = 0;

	skb_put(skb, sizeof(struct rsi_bt_rfmode_frame));

	status = common->priv->host_intf_ops->write_pkt(common->priv,
							skb->data,
							skb->len);
	dev_kfree_skb(skb);
	return status;
}
EXPORT_SYMBOL_GPL(rsi_send_rfmode_frame);

int rsi_deregister_bt(struct rsi_common *common)
{
	struct sk_buff *skb;
	struct rsi_bt_cmd_frame *cmd_frame;
	int status;

	rsi_dbg(MGMT_TX_ZONE, "%s: Sending BT register frame\n", __func__);

	skb = dev_alloc_skb(sizeof(struct rsi_bt_cmd_frame));
	if (!skb)
		return -ENOMEM;

	memset(skb->data, 0, sizeof(struct rsi_bt_cmd_frame));
	cmd_frame = (struct rsi_bt_cmd_frame *)skb->data;

	/* Length is 0 */
	cmd_frame->q_no = RSI_BT_MGMT_Q;
	cmd_frame->pkt_type = RSI_BT_PKT_TYPE_DEREGISTR;

	skb_put(skb, sizeof(struct rsi_bt_cmd_frame));

	//return rsi_coex_send_pkt(common, skb, RSI_BT_Q);
	status = common->priv->host_intf_ops->write_pkt(common->priv,
							skb->data,
							skb->len);
	dev_kfree_skb(skb);
	return status;
}
EXPORT_SYMBOL_GPL(rsi_deregister_bt);

int rsi_hci_recv_pkt(struct rsi_common *common, u8 *pkt)
{
	struct rsi_hci_adapter *h_adapter =
		(struct rsi_hci_adapter *)common->hci_adapter;
	struct sk_buff *skb = NULL;
	struct hci_dev *hdev = NULL;
	int pkt_len = rsi_get_length(pkt, 0);
	u8 queue_no = rsi_get_queueno(pkt, 0);

	if ((common->bt_fsm_state == BT_DEVICE_NOT_READY) &&
	    (pkt[14] == BT_CARD_READY_IND)) {
		rsi_dbg(INIT_ZONE, "%s: ===> BT Card Ready Received <===\n",
			__func__);

		if (common->suspend_in_prog) {
			rsi_dbg(INFO_ZONE,
				"Suspend is in prog; Do not process\n");
			return 0;
		}
		if (rsi_send_bt_reg_params(common)) {
			rsi_dbg(ERR_ZONE,
				"%s: Failed to write BT reg params\n",
				__func__);
		}
		rsi_dbg(INFO_ZONE, "Attaching HCI module\n");

		if (rsi_hci_attach(common)) {
			rsi_dbg(ERR_ZONE, "Failed to attach HCI module\n");
			return 0;
		}

		return 0;
	}

	if (common->bt_fsm_state != BT_DEVICE_READY) {
		rsi_dbg(INFO_ZONE, "BT Device not ready\n");
		return 0;
	}
	if (queue_no == RSI_BT_MGMT_Q) {
		u8 msg_type = pkt[14] & 0xFF;
	
		switch (msg_type) {
		case RESULT_CONFIRM:
			rsi_dbg(MGMT_RX_ZONE, "BT Result Confirm\n");
			return 0;
		case BT_BER:
			rsi_dbg(MGMT_RX_ZONE, "BT Ber\n");
			return 0;
		case BT_CW:
			rsi_dbg(MGMT_RX_ZONE, "BT CW\n");
			return 0;
		default:
			break;
		}
	}
	skb = dev_alloc_skb(pkt_len);
	if (!skb) {
		rsi_dbg(ERR_ZONE, "%s: Failed to alloc skb\n", __func__);
		return -ENOMEM;
	}
        hdev = h_adapter->hdev;
	memcpy(skb->data, pkt + FRAME_DESC_SZ, pkt_len);
	skb_put(skb, pkt_len);
	h_adapter->hdev->stat.byte_rx += skb->len;

	skb->dev = (void *)hdev;
	bt_cb(skb)->pkt_type = pkt[14];

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0)
	return hci_recv_frame(skb);
#else
	return hci_recv_frame(hdev, skb);
#endif
}
EXPORT_SYMBOL_GPL(rsi_hci_recv_pkt);

/**
 * rsi_hci_attach () - This function initializes HCI interface
 *				      
 * @common: Pointer to the driver private structure.
 *
 * Return: 0 on success, negative error code on failure
 */
int rsi_hci_attach(struct rsi_common *common)
{
	struct rsi_hci_adapter *h_adapter = NULL;
	struct hci_dev *hdev;
	int status = 0;

	/* Allocate HCI adapter */
	/* TODO: Check GFP_ATOMIC */
	h_adapter = kzalloc(sizeof (*h_adapter), GFP_KERNEL);
	if (!h_adapter) {
		rsi_dbg (ERR_ZONE, "Failed to alloc HCI adapter\n");
		return -ENOMEM;
	}
	h_adapter->priv = common;
	
	/* Create HCI Interface */
	hdev = hci_alloc_dev();
	if (!hdev) {
		rsi_dbg (ERR_ZONE, "Failed to alloc HCI device\n");
		goto err;
	}
	h_adapter->hdev = hdev;

	if (common->priv->rsi_host_intf == RSI_HOST_INTF_SDIO)
		hdev->bus = HCI_SDIO;
	else
		hdev->bus = HCI_USB;

	hci_set_drvdata(hdev, h_adapter);
#if LINUX_VERSION_CODE >= KERNEL_VERSION (4, 8, 0)
	hdev->dev_type = HCI_PRIMARY;
#else
	hdev->dev_type = HCI_BREDR;
#endif

	hdev->open = rsi_hci_open;
	hdev->close = rsi_hci_close;
	hdev->flush = rsi_hci_flush;
	hdev->send = rsi_hci_send_pkt;
#if LINUX_VERSION_CODE <= KERNEL_VERSION (3, 3, 8)
	hdev->destruct = rsi_hci_destruct;
	hdev->owner = THIS_MODULE;
#endif

        /* Initialize TX queue */
	skb_queue_head_init(&h_adapter->hci_tx_queue);
	common->hci_adapter = (void *)h_adapter;
	
	status = hci_register_dev(hdev);
	if (status < 0) {
		rsi_dbg(ERR_ZONE,
			"%s: HCI registration failed with errcode %d\n",
			__func__, status);
		goto err;
	}
	rsi_dbg(INIT_ZONE, "HCI Interface Created with name \'%s\'\n",
		hdev->name);
	common->bt_fsm_state = BT_DEVICE_READY;
	rsi_dbg(ERR_ZONE, " HCI module init done...\n");

	return 0;

err:
	if (hdev) {
		hci_unregister_dev(hdev);
		hci_free_dev(hdev);
		h_adapter->hdev = NULL;
	}
	kfree(h_adapter);

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(rsi_hci_attach);

/**
 * rsi_hci_attach () - This function initializes HCI interface
 *				      
 * @common: Pointer to the driver private structure.
 *
 * Return: 0 on success, negative error code on failure
 */
void rsi_hci_detach(struct rsi_common *common)
{
	struct rsi_hci_adapter *h_adapter = 
		(struct rsi_hci_adapter *)common->hci_adapter;
	struct hci_dev *hdev;

	rsi_dbg(INFO_ZONE, "Detaching HCI...\n");

	if (!h_adapter)
		return;

	hdev = h_adapter->hdev;
	if (hdev) {
                //hci_dev_hold(hdev);
		hci_unregister_dev(hdev);
                //hci_dev_put(hdev);
		hci_free_dev(hdev);
		h_adapter->hdev = NULL;
	}
	kfree(h_adapter);
	common->bt_fsm_state = BT_DEVICE_NOT_READY;

	return;
}
EXPORT_SYMBOL_GPL(rsi_hci_detach);