st_core.c 26.7 KB
Newer Older
P
Pavan Savoy 已提交
1 2 3
/*
 *  Shared Transport Line discipline driver Core
 *	This hooks up ST KIM driver and ST LL driver
4 5
 *  Copyright (C) 2009-2010 Texas Instruments
 *  Author: Pavan Savoy <pavan_savoy@ti.com>
P
Pavan Savoy 已提交
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
 *
 *  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>
32
#include <linux/ti_wilink_st.h>
P
Pavan Savoy 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

/* strings to be used for rfkill entries and by
 * ST Core to be used for sysfs debug entry
 */
#define PROTO_ENTRY(type, name)	name
const unsigned char *protocol_strngs[] = {
	PROTO_ENTRY(ST_BT, "Bluetooth"),
	PROTO_ENTRY(ST_FM, "FM"),
	PROTO_ENTRY(ST_GPS, "GPS"),
};
/* 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;
55
	pr_debug(" %s ", __func__);
P
Pavan Savoy 已提交
56 57 58 59 60 61 62 63 64
	for (i = 0; i < ST_MAX; i++) {
		if (st_gdata->list[i] != NULL)
			return ST_NOTEMPTY;
		/* not empty */
	}
	/* list empty */
	return ST_EMPTY;
}
#endif
65

P
Pavan Savoy 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
/* 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");
79
		return -1;
P
Pavan Savoy 已提交
80 81 82
	}
	tty = st_gdata->tty;
#ifdef VERBOSE
83 84
	print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
		16, 1, data, count, 0);
P
Pavan Savoy 已提交
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
#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)) {
112 113 114
		if (unlikely
			(st_gdata->list[protoid]->recv
			(st_gdata->list[protoid]->priv_data, st_gdata->rx_skb)
115
			     != 0)) {
P
Pavan Savoy 已提交
116 117 118 119 120 121 122 123 124 125 126
			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;
}

127 128
/**
 * st_reg_complete -
P
Pavan Savoy 已提交
129 130 131 132 133 134 135 136 137 138
 * 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))
139 140
			st_gdata->list[i]->reg_complete_cb
				(st_gdata->list[i]->priv_data, err);
P
Pavan Savoy 已提交
141 142 143 144 145 146 147 148
	}
}

static inline int st_check_data_len(struct st_data_s *st_gdata,
	int protoid, int len)
{
	register int room = skb_tailroom(st_gdata->rx_skb);

149
	pr_debug("len %d room %d", len, room);
P
Pavan Savoy 已提交
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

	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;
}

183 184 185
/**
 * st_wakeup_ack - internal function for action when wake-up ack
 *	received
P
Pavan Savoy 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
 */
static inline void st_wakeup_ack(struct st_data_s *st_gdata,
	unsigned char cmd)
{
	register struct sk_buff *waiting_skb;
	unsigned long flags = 0;

	spin_lock_irqsave(&st_gdata->lock, flags);
	/* de-Q from waitQ and Q in txQ now that the
	 * chip is awake
	 */
	while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
		skb_queue_tail(&st_gdata->txq, waiting_skb);

	/* state forwarded to ST LL */
	st_ll_sleep_state(st_gdata, (unsigned long)cmd);
	spin_unlock_irqrestore(&st_gdata->lock, flags);

	/* wake up to send the recently copied skbs from waitQ */
	st_tx_wakeup(st_gdata);
}

208 209 210 211 212 213 214
/**
 * st_int_recv - ST's internal receive function.
 *	Decodes received RAW data and forwards to corresponding
 *	client drivers (Bluetooth,FM,GPS..etc).
 *	This can receive various types of packets,
 *	HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
 *	CH-8 packets from FM, CH-9 packets from GPS cores.
P
Pavan Savoy 已提交
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
 */
void st_int_recv(void *disc_data,
	const unsigned char *data, long count)
{
	register char *ptr;
	struct hci_event_hdr *eh;
	struct hci_acl_hdr *ah;
	struct hci_sco_hdr *sh;
	struct fm_event_hdr *fm;
	struct gps_event_hdr *gps;
	register int len = 0, type = 0, dlen = 0;
	static enum proto_type protoid = ST_MAX;
	struct st_data_s *st_gdata = (struct st_data_s *)disc_data;

	ptr = (char *)data;
	/* tty_receive sent null ? */
	if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
		pr_err(" received null from TTY ");
		return;
	}

	pr_info("count %ld rx_state %ld"
		   "rx_count %ld", count, st_gdata->rx_state,
		   st_gdata->rx_count);

	/* Decode received bytes here */
	while (count) {
		if (st_gdata->rx_count) {
			len = min_t(unsigned int, st_gdata->rx_count, count);
			memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
			st_gdata->rx_count -= len;
			count -= len;
			ptr += len;

			if (st_gdata->rx_count)
				continue;

			/* Check ST RX state machine , where are we? */
			switch (st_gdata->rx_state) {

				/* Waiting for complete packet ? */
			case ST_BT_W4_DATA:
257
				pr_debug("Complete pkt received");
P
Pavan Savoy 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

				/* Ask ST CORE to forward
				 * the packet to protocol driver */
				st_send_frame(protoid, st_gdata);

				st_gdata->rx_state = ST_W4_PACKET_TYPE;
				st_gdata->rx_skb = NULL;
				protoid = ST_MAX;	/* is this required ? */
				continue;

				/* Waiting for Bluetooth event header ? */
			case ST_BT_W4_EVENT_HDR:
				eh = (struct hci_event_hdr *)st_gdata->rx_skb->
				    data;

273
				pr_debug("Event header: evt 0x%2.2x"
P
Pavan Savoy 已提交
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
					   "plen %d", eh->evt, eh->plen);

				st_check_data_len(st_gdata, protoid, eh->plen);
				continue;

				/* Waiting for Bluetooth acl header ? */
			case ST_BT_W4_ACL_HDR:
				ah = (struct hci_acl_hdr *)st_gdata->rx_skb->
				    data;
				dlen = __le16_to_cpu(ah->dlen);

				pr_info("ACL header: dlen %d", dlen);

				st_check_data_len(st_gdata, protoid, dlen);
				continue;

				/* Waiting for Bluetooth sco header ? */
			case ST_BT_W4_SCO_HDR:
				sh = (struct hci_sco_hdr *)st_gdata->rx_skb->
				    data;

				pr_info("SCO header: dlen %d", sh->dlen);

				st_check_data_len(st_gdata, protoid, sh->dlen);
				continue;
			case ST_FM_W4_EVENT_HDR:
				fm = (struct fm_event_hdr *)st_gdata->rx_skb->
				    data;
				pr_info("FM Header: ");
				st_check_data_len(st_gdata, ST_FM, fm->plen);
				continue;
				/* TODO : Add GPS packet machine logic here */
			case ST_GPS_W4_EVENT_HDR:
				/* [0x09 pkt hdr][R/W byte][2 byte len] */
				gps = (struct gps_event_hdr *)st_gdata->rx_skb->
				     data;
				pr_info("GPS Header: ");
				st_check_data_len(st_gdata, ST_GPS, gps->plen);
				continue;
			}	/* end of switch rx_state */
		}

		/* end of if rx_count */
		/* Check first byte of packet and identify module
		 * owner (BT/FM/GPS) */
		switch (*ptr) {

			/* Bluetooth event packet? */
		case HCI_EVENT_PKT:
			pr_info("Event packet");
			st_gdata->rx_state = ST_BT_W4_EVENT_HDR;
			st_gdata->rx_count = HCI_EVENT_HDR_SIZE;
			type = HCI_EVENT_PKT;
			protoid = ST_BT;
			break;

			/* Bluetooth acl packet? */
		case HCI_ACLDATA_PKT:
			pr_info("ACL packet");
			st_gdata->rx_state = ST_BT_W4_ACL_HDR;
			st_gdata->rx_count = HCI_ACL_HDR_SIZE;
			type = HCI_ACLDATA_PKT;
			protoid = ST_BT;
			break;

			/* Bluetooth sco packet? */
		case HCI_SCODATA_PKT:
			pr_info("SCO packet");
			st_gdata->rx_state = ST_BT_W4_SCO_HDR;
			st_gdata->rx_count = HCI_SCO_HDR_SIZE;
			type = HCI_SCODATA_PKT;
			protoid = ST_BT;
			break;

			/* Channel 8(FM) packet? */
		case ST_FM_CH8_PKT:
			pr_info("FM CH8 packet");
			type = ST_FM_CH8_PKT;
			st_gdata->rx_state = ST_FM_W4_EVENT_HDR;
			st_gdata->rx_count = FM_EVENT_HDR_SIZE;
			protoid = ST_FM;
			break;

			/* Channel 9(GPS) packet? */
		case 0x9:	/*ST_LL_GPS_CH9_PKT */
			pr_info("GPS CH9 packet");
			type = 0x9;	/* ST_LL_GPS_CH9_PKT; */
			protoid = ST_GPS;
			st_gdata->rx_state = ST_GPS_W4_EVENT_HDR;
			st_gdata->rx_count = 3;	/* GPS_EVENT_HDR_SIZE -1*/
			break;
		case LL_SLEEP_IND:
		case LL_SLEEP_ACK:
		case LL_WAKE_UP_IND:
			pr_info("PM packet");
			/* this takes appropriate action based on
			 * sleep state received --
			 */
			st_ll_sleep_state(st_gdata, *ptr);
			ptr++;
			count--;
			continue;
		case LL_WAKE_UP_ACK:
			pr_info("PM packet");
			/* wake up ack received */
			st_wakeup_ack(st_gdata, *ptr);
			ptr++;
			count--;
			continue;
			/* Unknow packet? */
		default:
			pr_err("Unknown packet type %2.2x", (__u8) *ptr);
			ptr++;
			count--;
			continue;
		};
		ptr++;
		count--;

		switch (protoid) {
		case ST_BT:
			/* Allocate new packet to hold received data */
			st_gdata->rx_skb =
			    bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
			if (!st_gdata->rx_skb) {
				pr_err("Can't allocate mem for new packet");
				st_gdata->rx_state = ST_W4_PACKET_TYPE;
				st_gdata->rx_count = 0;
				return;
			}
			bt_cb(st_gdata->rx_skb)->pkt_type = type;
			break;
		case ST_FM:	/* for FM */
			st_gdata->rx_skb =
			    alloc_skb(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
			if (!st_gdata->rx_skb) {
				pr_err("Can't allocate mem for new packet");
				st_gdata->rx_state = ST_W4_PACKET_TYPE;
				st_gdata->rx_count = 0;
				return;
			}
			/* place holder 0x08 */
			skb_reserve(st_gdata->rx_skb, 1);
			st_gdata->rx_skb->cb[0] = ST_FM_CH8_PKT;
			break;
		case ST_GPS:
			/* for GPS */
			st_gdata->rx_skb =
			    alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC);
			if (!st_gdata->rx_skb) {
				pr_err("Can't allocate mem for new packet");
				st_gdata->rx_state = ST_W4_PACKET_TYPE;
				st_gdata->rx_count = 0;
				return;
			}
			/* place holder 0x09 */
			skb_reserve(st_gdata->rx_skb, 1);
			st_gdata->rx_skb->cb[0] = 0x09;	/*ST_GPS_CH9_PKT; */
			break;
		case ST_MAX:
			break;
		}
	}
437
	pr_debug("done %s", __func__);
P
Pavan Savoy 已提交
438 439 440
	return;
}

441 442 443 444 445
/**
 * st_int_dequeue - internal de-Q function.
 *	If the previous data set was not written
 *	completely, return that skb which has the pending data.
 *	In normal cases, return top of txq.
P
Pavan Savoy 已提交
446 447 448 449 450
 */
struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
{
	struct sk_buff *returning_skb;

451
	pr_debug("%s", __func__);
P
Pavan Savoy 已提交
452 453 454 455 456 457 458 459
	if (st_gdata->tx_skb != NULL) {
		returning_skb = st_gdata->tx_skb;
		st_gdata->tx_skb = NULL;
		return returning_skb;
	}
	return skb_dequeue(&st_gdata->txq);
}

460 461 462 463 464 465 466 467
/**
 * st_int_enqueue - internal Q-ing function.
 *	Will either Q the skb to txq or the tx_waitq
 *	depending on the ST LL state.
 *	If the chip is asleep, then Q it onto waitq and
 *	wakeup the chip.
 *	txq and waitq needs protection since the other contexts
 *	may be sending data, waking up chip.
P
Pavan Savoy 已提交
468 469 470 471 472
 */
void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
{
	unsigned long flags = 0;

473
	pr_debug("%s", __func__);
P
Pavan Savoy 已提交
474 475 476 477 478 479 480 481 482 483
	spin_lock_irqsave(&st_gdata->lock, flags);

	switch (st_ll_getstate(st_gdata)) {
	case ST_LL_AWAKE:
		pr_info("ST LL is AWAKE, sending normally");
		skb_queue_tail(&st_gdata->txq, skb);
		break;
	case ST_LL_ASLEEP_TO_AWAKE:
		skb_queue_tail(&st_gdata->tx_waitq, skb);
		break;
484
	case ST_LL_AWAKE_TO_ASLEEP:
P
Pavan Savoy 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498
		pr_err("ST LL is illegal state(%ld),"
			   "purging received skb.", st_ll_getstate(st_gdata));
		kfree_skb(skb);
		break;
	case ST_LL_ASLEEP:
		skb_queue_tail(&st_gdata->tx_waitq, skb);
		st_ll_wakeup(st_gdata);
		break;
	default:
		pr_err("ST LL is illegal state(%ld),"
			   "purging received skb.", st_ll_getstate(st_gdata));
		kfree_skb(skb);
		break;
	}
499

P
Pavan Savoy 已提交
500
	spin_unlock_irqrestore(&st_gdata->lock, flags);
501
	pr_debug("done %s", __func__);
P
Pavan Savoy 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514
	return;
}

/*
 * internal wakeup function
 * called from either
 * - TTY layer when write's finished
 * - st_write (in context of the protocol stack)
 */
void st_tx_wakeup(struct st_data_s *st_data)
{
	struct sk_buff *skb;
	unsigned long flags;	/* for irq save flags */
515
	pr_debug("%s", __func__);
P
Pavan Savoy 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	/* check for sending & set flag sending here */
	if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
		pr_info("ST already sending");
		/* keep sending */
		set_bit(ST_TX_WAKEUP, &st_data->tx_state);
		return;
		/* TX_WAKEUP will be checked in another
		 * context
		 */
	}
	do {			/* come back if st_tx_wakeup is set */
		/* woke-up to write */
		clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
		while ((skb = st_int_dequeue(st_data))) {
			int len;
			spin_lock_irqsave(&st_data->lock, flags);
			/* enable wake-up from TTY */
			set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
			len = st_int_write(st_data, skb->data, skb->len);
			skb_pull(skb, len);
			/* if skb->len = len as expected, skb->len=0 */
			if (skb->len) {
				/* would be the next skb to be sent */
				st_data->tx_skb = skb;
				spin_unlock_irqrestore(&st_data->lock, flags);
				break;
			}
			kfree_skb(skb);
			spin_unlock_irqrestore(&st_data->lock, flags);
		}
		/* if wake-up is set in another context- restart sending */
	} while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));

	/* clear flag sending */
	clear_bit(ST_TX_SENDING, &st_data->tx_state);
}

/********************************************************************/
/* functions called from ST KIM
*/
556
void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
P
Pavan Savoy 已提交
557
{
558
	seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
559 560 561 562
			st_gdata->protos_registered,
			st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
			st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
			st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
P
Pavan Savoy 已提交
563 564 565 566 567 568 569 570 571 572
}

/********************************************************************/
/*
 * functions called from protocol stack drivers
 * to be EXPORT-ed
 */
long st_register(struct st_proto_s *new_proto)
{
	struct st_data_s	*st_gdata;
573
	long err = 0;
P
Pavan Savoy 已提交
574 575
	unsigned long flags = 0;

576
	st_kim_ref(&st_gdata, 0);
P
Pavan Savoy 已提交
577 578 579 580
	pr_info("%s(%d) ", __func__, new_proto->type);
	if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
	    || new_proto->reg_complete_cb == NULL) {
		pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
581
		return -1;
P
Pavan Savoy 已提交
582 583 584 585
	}

	if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
		pr_err("protocol %d not supported", new_proto->type);
586
		return -EPROTONOSUPPORT;
P
Pavan Savoy 已提交
587 588 589 590
	}

	if (st_gdata->list[new_proto->type] != NULL) {
		pr_err("protocol %d already registered", new_proto->type);
591
		return -EALREADY;
P
Pavan Savoy 已提交
592 593 594 595 596 597 598 599 600 601 602
	}

	/* can be from process context only */
	spin_lock_irqsave(&st_gdata->lock, flags);

	if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
		pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
		/* fw download in progress */
		st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);

		st_gdata->list[new_proto->type] = new_proto;
603
		st_gdata->protos_registered++;
P
Pavan Savoy 已提交
604 605 606 607
		new_proto->write = st_write;

		set_bit(ST_REG_PENDING, &st_gdata->st_state);
		spin_unlock_irqrestore(&st_gdata->lock, flags);
608
		return -EINPROGRESS;
P
Pavan Savoy 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621
	} else if (st_gdata->protos_registered == ST_EMPTY) {
		pr_info(" protocol list empty :%d ", new_proto->type);
		set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
		st_recv = st_kim_recv;

		/* release lock previously held - re-locked below */
		spin_unlock_irqrestore(&st_gdata->lock, flags);

		/* enable the ST LL - to set default chip state */
		st_ll_enable(st_gdata);
		/* this may take a while to complete
		 * since it involves BT fw download
		 */
622
		err = st_kim_start(st_gdata->kim_data);
623
		if (err != 0) {
P
Pavan Savoy 已提交
624 625 626 627
			clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
			if ((st_gdata->protos_registered != ST_EMPTY) &&
			    (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
				pr_err(" KIM failure complete callback ");
628
				st_reg_complete(st_gdata, -1);
P
Pavan Savoy 已提交
629 630
			}

631
			return -1;
P
Pavan Savoy 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645
		}

		/* the protocol might require other gpios to be toggled
		 */
		st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);

		clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
		st_recv = st_int_recv;

		/* this is where all pending registration
		 * are signalled to be complete by calling callback functions
		 */
		if ((st_gdata->protos_registered != ST_EMPTY) &&
		    (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
646
			pr_debug(" call reg complete callback ");
647
			st_reg_complete(st_gdata, 0);
P
Pavan Savoy 已提交
648 649 650 651 652 653 654 655 656
		}
		clear_bit(ST_REG_PENDING, &st_gdata->st_state);

		/* check for already registered once more,
		 * since the above check is old
		 */
		if (st_gdata->list[new_proto->type] != NULL) {
			pr_err(" proto %d already registered ",
				   new_proto->type);
657
			return -EALREADY;
P
Pavan Savoy 已提交
658 659 660 661
		}

		spin_lock_irqsave(&st_gdata->lock, flags);
		st_gdata->list[new_proto->type] = new_proto;
662
		st_gdata->protos_registered++;
P
Pavan Savoy 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
		new_proto->write = st_write;
		spin_unlock_irqrestore(&st_gdata->lock, flags);
		return err;
	}
	/* if fw is already downloaded & new stack registers protocol */
	else {
		switch (new_proto->type) {
		case ST_BT:
			/* do nothing */
			break;
		case ST_FM:
		case ST_GPS:
			st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
			break;
		case ST_MAX:
		default:
			pr_err("%d protocol not supported",
				   new_proto->type);
681 682
			spin_unlock_irqrestore(&st_gdata->lock, flags);
			return -EPROTONOSUPPORT;
P
Pavan Savoy 已提交
683 684
		}
		st_gdata->list[new_proto->type] = new_proto;
685
		st_gdata->protos_registered++;
P
Pavan Savoy 已提交
686 687 688 689 690 691
		new_proto->write = st_write;

		/* lock already held before entering else */
		spin_unlock_irqrestore(&st_gdata->lock, flags);
		return err;
	}
692
	pr_debug("done %s(%d) ", __func__, new_proto->type);
P
Pavan Savoy 已提交
693 694 695 696 697 698 699 700
}
EXPORT_SYMBOL_GPL(st_register);

/* to unregister a protocol -
 * to be called from protocol stack driver
 */
long st_unregister(enum proto_type type)
{
701
	long err = 0;
P
Pavan Savoy 已提交
702 703 704
	unsigned long flags = 0;
	struct st_data_s	*st_gdata;

705
	pr_debug("%s: %d ", __func__, type);
P
Pavan Savoy 已提交
706

707
	st_kim_ref(&st_gdata, 0);
P
Pavan Savoy 已提交
708 709
	if (type < ST_BT || type >= ST_MAX) {
		pr_err(" protocol %d not supported", type);
710
		return -EPROTONOSUPPORT;
P
Pavan Savoy 已提交
711 712 713 714 715 716 717
	}

	spin_lock_irqsave(&st_gdata->lock, flags);

	if (st_gdata->list[type] == NULL) {
		pr_err(" protocol %d not registered", type);
		spin_unlock_irqrestore(&st_gdata->lock, flags);
718
		return -EPROTONOSUPPORT;
P
Pavan Savoy 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	}

	st_gdata->protos_registered--;
	st_gdata->list[type] = NULL;

	/* kim ignores BT in the below function
	 * and handles the rest, BT is toggled
	 * only in kim_start and kim_stop
	 */
	st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
	spin_unlock_irqrestore(&st_gdata->lock, flags);

	if ((st_gdata->protos_registered == ST_EMPTY) &&
	    (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
		pr_info(" all protocols unregistered ");

		/* stop traffic on tty */
		if (st_gdata->tty) {
			tty_ldisc_flush(st_gdata->tty);
			stop_tty(st_gdata->tty);
		}

		/* all protocols now unregistered */
742
		st_kim_stop(st_gdata->kim_data);
P
Pavan Savoy 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
		/* disable ST LL */
		st_ll_disable(st_gdata);
	}
	return err;
}

/*
 * called in protocol stack drivers
 * via the write function pointer
 */
long st_write(struct sk_buff *skb)
{
	struct st_data_s *st_gdata;
#ifdef DEBUG
	enum proto_type protoid = ST_MAX;
#endif
	long len;

761
	st_kim_ref(&st_gdata, 0);
P
Pavan Savoy 已提交
762 763 764
	if (unlikely(skb == NULL || st_gdata == NULL
		|| st_gdata->tty == NULL)) {
		pr_err("data/tty unavailable to perform write");
765
		return -1;
P
Pavan Savoy 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	}
#ifdef DEBUG			/* open-up skb to read the 1st byte */
	switch (skb->data[0]) {
	case HCI_COMMAND_PKT:
	case HCI_ACLDATA_PKT:
	case HCI_SCODATA_PKT:
		protoid = ST_BT;
		break;
	case ST_FM_CH8_PKT:
		protoid = ST_FM;
		break;
	case 0x09:
		protoid = ST_GPS;
		break;
	}
	if (unlikely(st_gdata->list[protoid] == NULL)) {
		pr_err(" protocol %d not registered, and writing? ",
			   protoid);
784
		return -1;
P
Pavan Savoy 已提交
785 786
	}
#endif
787
	pr_debug("%d to be written", skb->len);
P
Pavan Savoy 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	len = skb->len;

	/* st_ll to decide where to enqueue the skb */
	st_int_enqueue(st_gdata, skb);
	/* wake up */
	st_tx_wakeup(st_gdata);

	/* return number of bytes written */
	return len;
}

/* for protocols making use of shared transport */
EXPORT_SYMBOL_GPL(st_unregister);

/********************************************************************/
/*
 * functions called from TTY layer
 */
static int st_tty_open(struct tty_struct *tty)
{
808
	int err = 0;
P
Pavan Savoy 已提交
809 810 811
	struct st_data_s *st_gdata;
	pr_info("%s ", __func__);

812
	st_kim_ref(&st_gdata, 0);
P
Pavan Savoy 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	st_gdata->tty = tty;
	tty->disc_data = st_gdata;

	/* don't do an wakeup for now */
	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);

	/* mem already allocated
	 */
	tty->receive_room = 65536;
	/* Flush any pending characters in the driver and discipline. */
	tty_ldisc_flush(tty);
	tty_driver_flush_buffer(tty);
	/*
	 * signal to UIM via KIM that -
	 * installation of N_TI_WL ldisc is complete
	 */
829
	st_kim_complete(st_gdata->kim_data);
830
	pr_debug("done %s", __func__);
P
Pavan Savoy 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
	return err;
}

static void st_tty_close(struct tty_struct *tty)
{
	unsigned char i = ST_MAX;
	unsigned long flags = 0;
	struct	st_data_s *st_gdata = tty->disc_data;

	pr_info("%s ", __func__);

	/* TODO:
	 * if a protocol has been registered & line discipline
	 * un-installed for some reason - what should be done ?
	 */
	spin_lock_irqsave(&st_gdata->lock, flags);
	for (i = ST_BT; i < ST_MAX; i++) {
		if (st_gdata->list[i] != NULL)
			pr_err("%d not un-registered", i);
		st_gdata->list[i] = NULL;
	}
852
	st_gdata->protos_registered = 0;
P
Pavan Savoy 已提交
853 854 855 856 857
	spin_unlock_irqrestore(&st_gdata->lock, flags);
	/*
	 * signal to UIM via KIM that -
	 * N_TI_WL ldisc is un-installed
	 */
858
	st_kim_complete(st_gdata->kim_data);
P
Pavan Savoy 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
	st_gdata->tty = NULL;
	/* Flush any pending characters in the driver and discipline. */
	tty_ldisc_flush(tty);
	tty_driver_flush_buffer(tty);

	spin_lock_irqsave(&st_gdata->lock, flags);
	/* empty out txq and tx_waitq */
	skb_queue_purge(&st_gdata->txq);
	skb_queue_purge(&st_gdata->tx_waitq);
	/* reset the TTY Rx states of ST */
	st_gdata->rx_count = 0;
	st_gdata->rx_state = ST_W4_PACKET_TYPE;
	kfree_skb(st_gdata->rx_skb);
	st_gdata->rx_skb = NULL;
	spin_unlock_irqrestore(&st_gdata->lock, flags);

875
	pr_debug("%s: done ", __func__);
P
Pavan Savoy 已提交
876 877 878 879 880 881 882
}

static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
			   char *tty_flags, int count)
{

#ifdef VERBOSE
883 884
	print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
		16, 1, data, count, 0);
P
Pavan Savoy 已提交
885 886 887 888 889 890 891
#endif

	/*
	 * if fw download is in progress then route incoming data
	 * to KIM for validation
	 */
	st_recv(tty->disc_data, data, count);
892
	pr_debug("done %s", __func__);
P
Pavan Savoy 已提交
893 894 895 896 897 898 899 900
}

/* wake-up function called in from the TTY layer
 * inside the internal wakeup function will be called
 */
static void st_tty_wakeup(struct tty_struct *tty)
{
	struct	st_data_s *st_gdata = tty->disc_data;
901
	pr_debug("%s ", __func__);
P
Pavan Savoy 已提交
902 903 904 905 906 907 908 909 910 911
	/* don't do an wakeup for now */
	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);

	/* call our internal wakeup */
	st_tx_wakeup((void *)st_gdata);
}

static void st_tty_flush_buffer(struct tty_struct *tty)
{
	struct	st_data_s *st_gdata = tty->disc_data;
912
	pr_debug("%s ", __func__);
P
Pavan Savoy 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950

	kfree_skb(st_gdata->tx_skb);
	st_gdata->tx_skb = NULL;

	tty->ops->flush_buffer(tty);
	return;
}

/********************************************************************/
int st_core_init(struct st_data_s **core_data)
{
	struct st_data_s *st_gdata;
	long err;
	static struct tty_ldisc_ops *st_ldisc_ops;

	/* populate and register to TTY line discipline */
	st_ldisc_ops = kzalloc(sizeof(*st_ldisc_ops), GFP_KERNEL);
	if (!st_ldisc_ops) {
		pr_err("no mem to allocate");
		return -ENOMEM;
	}

	st_ldisc_ops->magic = TTY_LDISC_MAGIC;
	st_ldisc_ops->name = "n_st";	/*"n_hci"; */
	st_ldisc_ops->open = st_tty_open;
	st_ldisc_ops->close = st_tty_close;
	st_ldisc_ops->receive_buf = st_tty_receive;
	st_ldisc_ops->write_wakeup = st_tty_wakeup;
	st_ldisc_ops->flush_buffer = st_tty_flush_buffer;
	st_ldisc_ops->owner = THIS_MODULE;

	err = tty_register_ldisc(N_TI_WL, st_ldisc_ops);
	if (err) {
		pr_err("error registering %d line discipline %ld",
			   N_TI_WL, err);
		kfree(st_ldisc_ops);
		return err;
	}
951
	pr_debug("registered n_shared line discipline");
P
Pavan Savoy 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

	st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
	if (!st_gdata) {
		pr_err("memory allocation failed");
		err = tty_unregister_ldisc(N_TI_WL);
		if (err)
			pr_err("unable to un-register ldisc %ld", err);
		kfree(st_ldisc_ops);
		err = -ENOMEM;
		return err;
	}

	/* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
	 * will be pushed in this queue for actual transmission.
	 */
	skb_queue_head_init(&st_gdata->txq);
	skb_queue_head_init(&st_gdata->tx_waitq);

	/* Locking used in st_int_enqueue() to avoid multiple execution */
	spin_lock_init(&st_gdata->lock);

	/* ldisc_ops ref to be only used in __exit of module */
	st_gdata->ldisc_ops = st_ldisc_ops;

#if 0
	err = st_kim_init();
	if (err) {
		pr_err("error during kim initialization(%ld)", err);
		kfree(st_gdata);
		err = tty_unregister_ldisc(N_TI_WL);
		if (err)
			pr_err("unable to un-register ldisc");
		kfree(st_ldisc_ops);
		return -1;
	}
#endif

	err = st_ll_init(st_gdata);
	if (err) {
		pr_err("error during st_ll initialization(%ld)", err);
		kfree(st_gdata);
		err = tty_unregister_ldisc(N_TI_WL);
		if (err)
			pr_err("unable to un-register ldisc");
		kfree(st_ldisc_ops);
		return -1;
	}
	*core_data = st_gdata;
	return 0;
}

void st_core_exit(struct st_data_s *st_gdata)
{
	long err;
	/* internal module cleanup */
	err = st_ll_deinit(st_gdata);
	if (err)
		pr_err("error during deinit of ST LL %ld", err);
#if 0
	err = st_kim_deinit();
	if (err)
		pr_err("error during deinit of ST KIM %ld", err);
#endif
	if (st_gdata != NULL) {
		/* Free ST Tx Qs and skbs */
		skb_queue_purge(&st_gdata->txq);
		skb_queue_purge(&st_gdata->tx_waitq);
		kfree_skb(st_gdata->rx_skb);
		kfree_skb(st_gdata->tx_skb);
		/* TTY ldisc cleanup */
		err = tty_unregister_ldisc(N_TI_WL);
		if (err)
			pr_err("unable to un-register ldisc %ld", err);
		kfree(st_gdata->ldisc_ops);
		/* free the global data pointer */
		kfree(st_gdata);
	}
}