st_core.c revision 320920cba355146258da7de80bed0069c1dff24a
1/*
2 *  Shared Transport Line discipline driver Core
3 *	This hooks up ST KIM driver and ST LL driver
4 *  Copyright (C) 2009 Texas Instruments
5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License version 2 as
8 *  published by the Free Software Foundation.
9 *
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU General Public License for more details.
14 *
15 *  You should have received a copy of the GNU General Public License
16 *  along with this program; if not, write to the Free Software
17 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 *
19 */
20
21#define pr_fmt(fmt)	"(stc): " fmt
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/tty.h>
26
27/* understand BT, FM and GPS for now */
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30#include <net/bluetooth/hci.h>
31#include "fm.h"
32/*
33 * packet formats for fm and gps
34 * #include "gps.h"
35 */
36#include "st_core.h"
37#include "st_kim.h"
38#include "st_ll.h"
39#include "st.h"
40
41#ifdef DEBUG
42/* strings to be used for rfkill entries and by
43 * ST Core to be used for sysfs debug entry
44 */
45#define PROTO_ENTRY(type, name)	name
46const unsigned char *protocol_strngs[] = {
47	PROTO_ENTRY(ST_BT, "Bluetooth"),
48	PROTO_ENTRY(ST_FM, "FM"),
49	PROTO_ENTRY(ST_GPS, "GPS"),
50};
51#endif
52/* function pointer pointing to either,
53 * st_kim_recv during registration to receive fw download responses
54 * st_int_recv after registration to receive proto stack responses
55 */
56void (*st_recv) (void*, const unsigned char*, long);
57
58/********************************************************************/
59#if 0
60/* internal misc functions */
61bool is_protocol_list_empty(void)
62{
63	unsigned char i = 0;
64	pr_info(" %s ", __func__);
65	for (i = 0; i < ST_MAX; i++) {
66		if (st_gdata->list[i] != NULL)
67			return ST_NOTEMPTY;
68		/* not empty */
69	}
70	/* list empty */
71	return ST_EMPTY;
72}
73#endif
74/* can be called in from
75 * -- KIM (during fw download)
76 * -- ST Core (during st_write)
77 *
78 *  This is the internal write function - a wrapper
79 *  to tty->ops->write
80 */
81int st_int_write(struct st_data_s *st_gdata,
82	const unsigned char *data, int count)
83{
84#ifdef VERBOSE			/* for debug */
85	int i;
86#endif
87	struct tty_struct *tty;
88	if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
89		pr_err("tty unavailable to perform write");
90		return -1;
91	}
92	tty = st_gdata->tty;
93#ifdef VERBOSE
94	printk(KERN_ERR "start data..\n");
95	for (i = 0; i < count; i++)	/* no newlines for each datum */
96		printk(" %x", data[i]);
97	printk(KERN_ERR "\n ..end data\n");
98#endif
99	return tty->ops->write(tty, data, count);
100
101}
102
103/*
104 * push the skb received to relevant
105 * protocol stacks
106 */
107void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
108{
109	pr_info(" %s(prot:%d) ", __func__, protoid);
110
111	if (unlikely
112	    (st_gdata == NULL || st_gdata->rx_skb == NULL
113	     || st_gdata->list[protoid] == NULL)) {
114		pr_err("protocol %d not registered, no data to send?",
115			   protoid);
116		kfree_skb(st_gdata->rx_skb);
117		return;
118	}
119	/* this cannot fail
120	 * this shouldn't take long
121	 * - should be just skb_queue_tail for the
122	 *   protocol stack driver
123	 */
124	if (likely(st_gdata->list[protoid]->recv != NULL)) {
125		if (unlikely(st_gdata->list[protoid]->recv(st_gdata->rx_skb)
126			     != 0)) {
127			pr_err(" proto stack %d's ->recv failed", protoid);
128			kfree_skb(st_gdata->rx_skb);
129			return;
130		}
131	} else {
132		pr_err(" proto stack %d's ->recv null", protoid);
133		kfree_skb(st_gdata->rx_skb);
134	}
135	pr_info(" done %s", __func__);
136	return;
137}
138
139/*
140 * to call registration complete callbacks
141 * of all protocol stack drivers
142 */
143void st_reg_complete(struct st_data_s *st_gdata, char err)
144{
145	unsigned char i = 0;
146	pr_info(" %s ", __func__);
147	for (i = 0; i < ST_MAX; i++) {
148		if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
149			   st_gdata->list[i]->reg_complete_cb != NULL))
150			st_gdata->list[i]->reg_complete_cb(err);
151	}
152}
153
154static inline int st_check_data_len(struct st_data_s *st_gdata,
155	int protoid, int len)
156{
157	register int room = skb_tailroom(st_gdata->rx_skb);
158
159	pr_info("len %d room %d", len, room);
160
161	if (!len) {
162		/* Received packet has only packet header and
163		 * has zero length payload. So, ask ST CORE to
164		 * forward the packet to protocol driver (BT/FM/GPS)
165		 */
166		st_send_frame(protoid, st_gdata);
167
168	} else if (len > room) {
169		/* Received packet's payload length is larger.
170		 * We can't accommodate it in created skb.
171		 */
172		pr_err("Data length is too large len %d room %d", len,
173			   room);
174		kfree_skb(st_gdata->rx_skb);
175	} else {
176		/* Packet header has non-zero payload length and
177		 * we have enough space in created skb. Lets read
178		 * payload data */
179		st_gdata->rx_state = ST_BT_W4_DATA;
180		st_gdata->rx_count = len;
181		return len;
182	}
183
184	/* Change ST state to continue to process next
185	 * packet */
186	st_gdata->rx_state = ST_W4_PACKET_TYPE;
187	st_gdata->rx_skb = NULL;
188	st_gdata->rx_count = 0;
189
190	return 0;
191}
192
193/* internal function for action when wake-up ack
194 * received
195 */
196static inline void st_wakeup_ack(struct st_data_s *st_gdata,
197	unsigned char cmd)
198{
199	register struct sk_buff *waiting_skb;
200	unsigned long flags = 0;
201
202	spin_lock_irqsave(&st_gdata->lock, flags);
203	/* de-Q from waitQ and Q in txQ now that the
204	 * chip is awake
205	 */
206	while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
207		skb_queue_tail(&st_gdata->txq, waiting_skb);
208
209	/* state forwarded to ST LL */
210	st_ll_sleep_state(st_gdata, (unsigned long)cmd);
211	spin_unlock_irqrestore(&st_gdata->lock, flags);
212
213	/* wake up to send the recently copied skbs from waitQ */
214	st_tx_wakeup(st_gdata);
215}
216
217/* Decodes received RAW data and forwards to corresponding
218 * client drivers (Bluetooth,FM,GPS..etc).
219 *
220 */
221void st_int_recv(void *disc_data,
222	const unsigned char *data, long count)
223{
224	register char *ptr;
225	struct hci_event_hdr *eh;
226	struct hci_acl_hdr *ah;
227	struct hci_sco_hdr *sh;
228	struct fm_event_hdr *fm;
229	struct gps_event_hdr *gps;
230	register int len = 0, type = 0, dlen = 0;
231	static enum proto_type protoid = ST_MAX;
232	struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
233
234	ptr = (char *)data;
235	/* tty_receive sent null ? */
236	if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
237		pr_err(" received null from TTY ");
238		return;
239	}
240
241	pr_info("count %ld rx_state %ld"
242		   "rx_count %ld", count, st_gdata->rx_state,
243		   st_gdata->rx_count);
244
245	/* Decode received bytes here */
246	while (count) {
247		if (st_gdata->rx_count) {
248			len = min_t(unsigned int, st_gdata->rx_count, count);
249			memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
250			st_gdata->rx_count -= len;
251			count -= len;
252			ptr += len;
253
254			if (st_gdata->rx_count)
255				continue;
256
257			/* Check ST RX state machine , where are we? */
258			switch (st_gdata->rx_state) {
259
260				/* Waiting for complete packet ? */
261			case ST_BT_W4_DATA:
262				pr_info("Complete pkt received");
263
264				/* Ask ST CORE to forward
265				 * the packet to protocol driver */
266				st_send_frame(protoid, st_gdata);
267
268				st_gdata->rx_state = ST_W4_PACKET_TYPE;
269				st_gdata->rx_skb = NULL;
270				protoid = ST_MAX;	/* is this required ? */
271				continue;
272
273				/* Waiting for Bluetooth event header ? */
274			case ST_BT_W4_EVENT_HDR:
275				eh = (struct hci_event_hdr *)st_gdata->rx_skb->
276				    data;
277
278				pr_info("Event header: evt 0x%2.2x"
279					   "plen %d", eh->evt, eh->plen);
280
281				st_check_data_len(st_gdata, protoid, eh->plen);
282				continue;
283
284				/* Waiting for Bluetooth acl header ? */
285			case ST_BT_W4_ACL_HDR:
286				ah = (struct hci_acl_hdr *)st_gdata->rx_skb->
287				    data;
288				dlen = __le16_to_cpu(ah->dlen);
289
290				pr_info("ACL header: dlen %d", dlen);
291
292				st_check_data_len(st_gdata, protoid, dlen);
293				continue;
294
295				/* Waiting for Bluetooth sco header ? */
296			case ST_BT_W4_SCO_HDR:
297				sh = (struct hci_sco_hdr *)st_gdata->rx_skb->
298				    data;
299
300				pr_info("SCO header: dlen %d", sh->dlen);
301
302				st_check_data_len(st_gdata, protoid, sh->dlen);
303				continue;
304			case ST_FM_W4_EVENT_HDR:
305				fm = (struct fm_event_hdr *)st_gdata->rx_skb->
306				    data;
307				pr_info("FM Header: ");
308				st_check_data_len(st_gdata, ST_FM, fm->plen);
309				continue;
310				/* TODO : Add GPS packet machine logic here */
311			case ST_GPS_W4_EVENT_HDR:
312				/* [0x09 pkt hdr][R/W byte][2 byte len] */
313				gps = (struct gps_event_hdr *)st_gdata->rx_skb->
314				     data;
315				pr_info("GPS Header: ");
316				st_check_data_len(st_gdata, ST_GPS, gps->plen);
317				continue;
318			}	/* end of switch rx_state */
319		}
320
321		/* end of if rx_count */
322		/* Check first byte of packet and identify module
323		 * owner (BT/FM/GPS) */
324		switch (*ptr) {
325
326			/* Bluetooth event packet? */
327		case HCI_EVENT_PKT:
328			pr_info("Event packet");
329			st_gdata->rx_state = ST_BT_W4_EVENT_HDR;
330			st_gdata->rx_count = HCI_EVENT_HDR_SIZE;
331			type = HCI_EVENT_PKT;
332			protoid = ST_BT;
333			break;
334
335			/* Bluetooth acl packet? */
336		case HCI_ACLDATA_PKT:
337			pr_info("ACL packet");
338			st_gdata->rx_state = ST_BT_W4_ACL_HDR;
339			st_gdata->rx_count = HCI_ACL_HDR_SIZE;
340			type = HCI_ACLDATA_PKT;
341			protoid = ST_BT;
342			break;
343
344			/* Bluetooth sco packet? */
345		case HCI_SCODATA_PKT:
346			pr_info("SCO packet");
347			st_gdata->rx_state = ST_BT_W4_SCO_HDR;
348			st_gdata->rx_count = HCI_SCO_HDR_SIZE;
349			type = HCI_SCODATA_PKT;
350			protoid = ST_BT;
351			break;
352
353			/* Channel 8(FM) packet? */
354		case ST_FM_CH8_PKT:
355			pr_info("FM CH8 packet");
356			type = ST_FM_CH8_PKT;
357			st_gdata->rx_state = ST_FM_W4_EVENT_HDR;
358			st_gdata->rx_count = FM_EVENT_HDR_SIZE;
359			protoid = ST_FM;
360			break;
361
362			/* Channel 9(GPS) packet? */
363		case 0x9:	/*ST_LL_GPS_CH9_PKT */
364			pr_info("GPS CH9 packet");
365			type = 0x9;	/* ST_LL_GPS_CH9_PKT; */
366			protoid = ST_GPS;
367			st_gdata->rx_state = ST_GPS_W4_EVENT_HDR;
368			st_gdata->rx_count = 3;	/* GPS_EVENT_HDR_SIZE -1*/
369			break;
370		case LL_SLEEP_IND:
371		case LL_SLEEP_ACK:
372		case LL_WAKE_UP_IND:
373			pr_info("PM packet");
374			/* this takes appropriate action based on
375			 * sleep state received --
376			 */
377			st_ll_sleep_state(st_gdata, *ptr);
378			ptr++;
379			count--;
380			continue;
381		case LL_WAKE_UP_ACK:
382			pr_info("PM packet");
383			/* wake up ack received */
384			st_wakeup_ack(st_gdata, *ptr);
385			ptr++;
386			count--;
387			continue;
388			/* Unknow packet? */
389		default:
390			pr_err("Unknown packet type %2.2x", (__u8) *ptr);
391			ptr++;
392			count--;
393			continue;
394		};
395		ptr++;
396		count--;
397
398		switch (protoid) {
399		case ST_BT:
400			/* Allocate new packet to hold received data */
401			st_gdata->rx_skb =
402			    bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
403			if (!st_gdata->rx_skb) {
404				pr_err("Can't allocate mem for new packet");
405				st_gdata->rx_state = ST_W4_PACKET_TYPE;
406				st_gdata->rx_count = 0;
407				return;
408			}
409			bt_cb(st_gdata->rx_skb)->pkt_type = type;
410			break;
411		case ST_FM:	/* for FM */
412			st_gdata->rx_skb =
413			    alloc_skb(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
414			if (!st_gdata->rx_skb) {
415				pr_err("Can't allocate mem for new packet");
416				st_gdata->rx_state = ST_W4_PACKET_TYPE;
417				st_gdata->rx_count = 0;
418				return;
419			}
420			/* place holder 0x08 */
421			skb_reserve(st_gdata->rx_skb, 1);
422			st_gdata->rx_skb->cb[0] = ST_FM_CH8_PKT;
423			break;
424		case ST_GPS:
425			/* for GPS */
426			st_gdata->rx_skb =
427			    alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC);
428			if (!st_gdata->rx_skb) {
429				pr_err("Can't allocate mem for new packet");
430				st_gdata->rx_state = ST_W4_PACKET_TYPE;
431				st_gdata->rx_count = 0;
432				return;
433			}
434			/* place holder 0x09 */
435			skb_reserve(st_gdata->rx_skb, 1);
436			st_gdata->rx_skb->cb[0] = 0x09;	/*ST_GPS_CH9_PKT; */
437			break;
438		case ST_MAX:
439			break;
440		}
441	}
442	pr_info("done %s", __func__);
443	return;
444}
445
446/* internal de-Q function
447 * -- return previous in-completely written skb
448 *  or return the skb in the txQ
449 */
450struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
451{
452	struct sk_buff *returning_skb;
453
454	pr_info("%s", __func__);
455	/* if the previous skb wasn't written completely
456	 */
457	if (st_gdata->tx_skb != NULL) {
458		returning_skb = st_gdata->tx_skb;
459		st_gdata->tx_skb = NULL;
460		return returning_skb;
461	}
462
463	/* de-Q from the txQ always if previous write is complete */
464	return skb_dequeue(&st_gdata->txq);
465}
466
467/* internal Q-ing function
468 * will either Q the skb to txq or the tx_waitq
469 * depending on the ST LL state
470 *
471 * lock the whole func - since ll_getstate and Q-ing should happen
472 * in one-shot
473 */
474void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
475{
476	unsigned long flags = 0;
477
478	pr_info("%s", __func__);
479	/* this function can be invoked in more then one context.
480	 * so have a lock */
481	spin_lock_irqsave(&st_gdata->lock, flags);
482
483	switch (st_ll_getstate(st_gdata)) {
484	case ST_LL_AWAKE:
485		pr_info("ST LL is AWAKE, sending normally");
486		skb_queue_tail(&st_gdata->txq, skb);
487		break;
488	case ST_LL_ASLEEP_TO_AWAKE:
489		skb_queue_tail(&st_gdata->tx_waitq, skb);
490		break;
491	case ST_LL_AWAKE_TO_ASLEEP:	/* host cannot be in this state */
492		pr_err("ST LL is illegal state(%ld),"
493			   "purging received skb.", st_ll_getstate(st_gdata));
494		kfree_skb(skb);
495		break;
496
497	case ST_LL_ASLEEP:
498		/* call a function of ST LL to put data
499		 * in tx_waitQ and wake_ind in txQ
500		 */
501		skb_queue_tail(&st_gdata->tx_waitq, skb);
502		st_ll_wakeup(st_gdata);
503		break;
504	default:
505		pr_err("ST LL is illegal state(%ld),"
506			   "purging received skb.", st_ll_getstate(st_gdata));
507		kfree_skb(skb);
508		break;
509	}
510	spin_unlock_irqrestore(&st_gdata->lock, flags);
511	pr_info("done %s", __func__);
512	return;
513}
514
515/*
516 * internal wakeup function
517 * called from either
518 * - TTY layer when write's finished
519 * - st_write (in context of the protocol stack)
520 */
521void st_tx_wakeup(struct st_data_s *st_data)
522{
523	struct sk_buff *skb;
524	unsigned long flags;	/* for irq save flags */
525	pr_info("%s", __func__);
526	/* check for sending & set flag sending here */
527	if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
528		pr_info("ST already sending");
529		/* keep sending */
530		set_bit(ST_TX_WAKEUP, &st_data->tx_state);
531		return;
532		/* TX_WAKEUP will be checked in another
533		 * context
534		 */
535	}
536	do {			/* come back if st_tx_wakeup is set */
537		/* woke-up to write */
538		clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
539		while ((skb = st_int_dequeue(st_data))) {
540			int len;
541			spin_lock_irqsave(&st_data->lock, flags);
542			/* enable wake-up from TTY */
543			set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
544			len = st_int_write(st_data, skb->data, skb->len);
545			skb_pull(skb, len);
546			/* if skb->len = len as expected, skb->len=0 */
547			if (skb->len) {
548				/* would be the next skb to be sent */
549				st_data->tx_skb = skb;
550				spin_unlock_irqrestore(&st_data->lock, flags);
551				break;
552			}
553			kfree_skb(skb);
554			spin_unlock_irqrestore(&st_data->lock, flags);
555		}
556		/* if wake-up is set in another context- restart sending */
557	} while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
558
559	/* clear flag sending */
560	clear_bit(ST_TX_SENDING, &st_data->tx_state);
561}
562
563/********************************************************************/
564/* functions called from ST KIM
565*/
566void kim_st_list_protocols(struct st_data_s *st_gdata, char *buf)
567{
568	unsigned long flags = 0;
569#ifdef DEBUG
570	unsigned char i = ST_MAX;
571#endif
572	spin_lock_irqsave(&st_gdata->lock, flags);
573#ifdef DEBUG			/* more detailed log */
574	for (i = 0; i < ST_MAX; i++) {
575		if (i == 0) {
576			sprintf(buf, "%s is %s", protocol_strngs[i],
577				st_gdata->list[i] !=
578				NULL ? "Registered" : "Unregistered");
579		} else {
580			sprintf(buf, "%s\n%s is %s", buf, protocol_strngs[i],
581				st_gdata->list[i] !=
582				NULL ? "Registered" : "Unregistered");
583		}
584	}
585	sprintf(buf, "%s\n", buf);
586#else /* limited info */
587	sprintf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
588			st_gdata->protos_registered,
589			st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
590			st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
591			st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
592#endif
593	spin_unlock_irqrestore(&st_gdata->lock, flags);
594}
595
596/********************************************************************/
597/*
598 * functions called from protocol stack drivers
599 * to be EXPORT-ed
600 */
601long st_register(struct st_proto_s *new_proto)
602{
603	struct st_data_s	*st_gdata;
604	long err = 0;
605	unsigned long flags = 0;
606
607	st_kim_ref(&st_gdata);
608	pr_info("%s(%d) ", __func__, new_proto->type);
609	if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
610	    || new_proto->reg_complete_cb == NULL) {
611		pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
612		return -1;
613	}
614
615	if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
616		pr_err("protocol %d not supported", new_proto->type);
617		return -EPROTONOSUPPORT;
618	}
619
620	if (st_gdata->list[new_proto->type] != NULL) {
621		pr_err("protocol %d already registered", new_proto->type);
622		return -EALREADY;
623	}
624
625	/* can be from process context only */
626	spin_lock_irqsave(&st_gdata->lock, flags);
627
628	if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
629		pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
630		/* fw download in progress */
631		st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
632
633		st_gdata->list[new_proto->type] = new_proto;
634		st_gdata->protos_registered++;
635		new_proto->write = st_write;
636
637		set_bit(ST_REG_PENDING, &st_gdata->st_state);
638		spin_unlock_irqrestore(&st_gdata->lock, flags);
639		return -EINPROGRESS;
640	} else if (st_gdata->protos_registered == ST_EMPTY) {
641		pr_info(" protocol list empty :%d ", new_proto->type);
642		set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
643		st_recv = st_kim_recv;
644
645		/* release lock previously held - re-locked below */
646		spin_unlock_irqrestore(&st_gdata->lock, flags);
647
648		/* enable the ST LL - to set default chip state */
649		st_ll_enable(st_gdata);
650		/* this may take a while to complete
651		 * since it involves BT fw download
652		 */
653		err = st_kim_start(st_gdata->kim_data);
654		if (err != 0) {
655			clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
656			if ((st_gdata->protos_registered != ST_EMPTY) &&
657			    (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
658				pr_err(" KIM failure complete callback ");
659				st_reg_complete(st_gdata, -1);
660			}
661
662			return -1;
663		}
664
665		/* the protocol might require other gpios to be toggled
666		 */
667		st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
668
669		clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
670		st_recv = st_int_recv;
671
672		/* this is where all pending registration
673		 * are signalled to be complete by calling callback functions
674		 */
675		if ((st_gdata->protos_registered != ST_EMPTY) &&
676		    (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
677			pr_info(" call reg complete callback ");
678			st_reg_complete(st_gdata, 0);
679		}
680		clear_bit(ST_REG_PENDING, &st_gdata->st_state);
681
682		/* check for already registered once more,
683		 * since the above check is old
684		 */
685		if (st_gdata->list[new_proto->type] != NULL) {
686			pr_err(" proto %d already registered ",
687				   new_proto->type);
688			return -EALREADY;
689		}
690
691		spin_lock_irqsave(&st_gdata->lock, flags);
692		st_gdata->list[new_proto->type] = new_proto;
693		st_gdata->protos_registered++;
694		new_proto->write = st_write;
695		spin_unlock_irqrestore(&st_gdata->lock, flags);
696		return err;
697	}
698	/* if fw is already downloaded & new stack registers protocol */
699	else {
700		switch (new_proto->type) {
701		case ST_BT:
702			/* do nothing */
703			break;
704		case ST_FM:
705		case ST_GPS:
706			st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
707			break;
708		case ST_MAX:
709		default:
710			pr_err("%d protocol not supported",
711				   new_proto->type);
712			err = -EPROTONOSUPPORT;
713			/* something wrong */
714			break;
715		}
716		st_gdata->list[new_proto->type] = new_proto;
717		st_gdata->protos_registered++;
718		new_proto->write = st_write;
719
720		/* lock already held before entering else */
721		spin_unlock_irqrestore(&st_gdata->lock, flags);
722		return err;
723	}
724	pr_info("done %s(%d) ", __func__, new_proto->type);
725}
726EXPORT_SYMBOL_GPL(st_register);
727
728/* to unregister a protocol -
729 * to be called from protocol stack driver
730 */
731long st_unregister(enum proto_type type)
732{
733	long err = 0;
734	unsigned long flags = 0;
735	struct st_data_s	*st_gdata;
736
737	pr_info("%s: %d ", __func__, type);
738
739	st_kim_ref(&st_gdata);
740	if (type < ST_BT || type >= ST_MAX) {
741		pr_err(" protocol %d not supported", type);
742		return -EPROTONOSUPPORT;
743	}
744
745	spin_lock_irqsave(&st_gdata->lock, flags);
746
747	if (st_gdata->list[type] == NULL) {
748		pr_err(" protocol %d not registered", type);
749		spin_unlock_irqrestore(&st_gdata->lock, flags);
750		return -EPROTONOSUPPORT;
751	}
752
753	st_gdata->protos_registered--;
754	st_gdata->list[type] = NULL;
755
756	/* kim ignores BT in the below function
757	 * and handles the rest, BT is toggled
758	 * only in kim_start and kim_stop
759	 */
760	st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
761	spin_unlock_irqrestore(&st_gdata->lock, flags);
762
763	if ((st_gdata->protos_registered == ST_EMPTY) &&
764	    (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
765		pr_info(" all protocols unregistered ");
766
767		/* stop traffic on tty */
768		if (st_gdata->tty) {
769			tty_ldisc_flush(st_gdata->tty);
770			stop_tty(st_gdata->tty);
771		}
772
773		/* all protocols now unregistered */
774		st_kim_stop(st_gdata->kim_data);
775		/* disable ST LL */
776		st_ll_disable(st_gdata);
777	}
778	return err;
779}
780
781/*
782 * called in protocol stack drivers
783 * via the write function pointer
784 */
785long st_write(struct sk_buff *skb)
786{
787	struct st_data_s *st_gdata;
788#ifdef DEBUG
789	enum proto_type protoid = ST_MAX;
790#endif
791	long len;
792
793	st_kim_ref(&st_gdata);
794	if (unlikely(skb == NULL || st_gdata == NULL
795		|| st_gdata->tty == NULL)) {
796		pr_err("data/tty unavailable to perform write");
797		return -1;
798	}
799#ifdef DEBUG			/* open-up skb to read the 1st byte */
800	switch (skb->data[0]) {
801	case HCI_COMMAND_PKT:
802	case HCI_ACLDATA_PKT:
803	case HCI_SCODATA_PKT:
804		protoid = ST_BT;
805		break;
806	case ST_FM_CH8_PKT:
807		protoid = ST_FM;
808		break;
809	case 0x09:
810		protoid = ST_GPS;
811		break;
812	}
813	if (unlikely(st_gdata->list[protoid] == NULL)) {
814		pr_err(" protocol %d not registered, and writing? ",
815			   protoid);
816		return -1;
817	}
818#endif
819	pr_info("%d to be written", skb->len);
820	len = skb->len;
821
822	/* st_ll to decide where to enqueue the skb */
823	st_int_enqueue(st_gdata, skb);
824	/* wake up */
825	st_tx_wakeup(st_gdata);
826
827	/* return number of bytes written */
828	return len;
829}
830
831/* for protocols making use of shared transport */
832EXPORT_SYMBOL_GPL(st_unregister);
833
834/********************************************************************/
835/*
836 * functions called from TTY layer
837 */
838static int st_tty_open(struct tty_struct *tty)
839{
840	int err = 0;
841	struct st_data_s *st_gdata;
842	pr_info("%s ", __func__);
843
844	st_kim_ref(&st_gdata);
845	st_gdata->tty = tty;
846	tty->disc_data = st_gdata;
847
848	/* don't do an wakeup for now */
849	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
850
851	/* mem already allocated
852	 */
853	tty->receive_room = 65536;
854	/* Flush any pending characters in the driver and discipline. */
855	tty_ldisc_flush(tty);
856	tty_driver_flush_buffer(tty);
857	/*
858	 * signal to UIM via KIM that -
859	 * installation of N_TI_WL ldisc is complete
860	 */
861	st_kim_complete(st_gdata->kim_data);
862	pr_info("done %s", __func__);
863	return err;
864}
865
866static void st_tty_close(struct tty_struct *tty)
867{
868	unsigned char i = ST_MAX;
869	unsigned long flags = 0;
870	struct	st_data_s *st_gdata = tty->disc_data;
871
872	pr_info("%s ", __func__);
873
874	/* TODO:
875	 * if a protocol has been registered & line discipline
876	 * un-installed for some reason - what should be done ?
877	 */
878	spin_lock_irqsave(&st_gdata->lock, flags);
879	for (i = ST_BT; i < ST_MAX; i++) {
880		if (st_gdata->list[i] != NULL)
881			pr_err("%d not un-registered", i);
882		st_gdata->list[i] = NULL;
883	}
884	spin_unlock_irqrestore(&st_gdata->lock, flags);
885	/*
886	 * signal to UIM via KIM that -
887	 * N_TI_WL ldisc is un-installed
888	 */
889	st_kim_complete(st_gdata->kim_data);
890	st_gdata->tty = NULL;
891	/* Flush any pending characters in the driver and discipline. */
892	tty_ldisc_flush(tty);
893	tty_driver_flush_buffer(tty);
894
895	spin_lock_irqsave(&st_gdata->lock, flags);
896	/* empty out txq and tx_waitq */
897	skb_queue_purge(&st_gdata->txq);
898	skb_queue_purge(&st_gdata->tx_waitq);
899	/* reset the TTY Rx states of ST */
900	st_gdata->rx_count = 0;
901	st_gdata->rx_state = ST_W4_PACKET_TYPE;
902	kfree_skb(st_gdata->rx_skb);
903	st_gdata->rx_skb = NULL;
904	spin_unlock_irqrestore(&st_gdata->lock, flags);
905
906	pr_info("%s: done ", __func__);
907}
908
909static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
910			   char *tty_flags, int count)
911{
912
913#ifdef VERBOSE
914	long i;
915	printk(KERN_ERR "incoming data...\n");
916	for (i = 0; i < count; i++)
917		printk(" %x", data[i]);
918	printk(KERN_ERR "\n.. data end\n");
919#endif
920
921	/*
922	 * if fw download is in progress then route incoming data
923	 * to KIM for validation
924	 */
925	st_recv(tty->disc_data, data, count);
926	pr_info("done %s", __func__);
927}
928
929/* wake-up function called in from the TTY layer
930 * inside the internal wakeup function will be called
931 */
932static void st_tty_wakeup(struct tty_struct *tty)
933{
934	struct	st_data_s *st_gdata = tty->disc_data;
935	pr_info("%s ", __func__);
936	/* don't do an wakeup for now */
937	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
938
939	/* call our internal wakeup */
940	st_tx_wakeup((void *)st_gdata);
941}
942
943static void st_tty_flush_buffer(struct tty_struct *tty)
944{
945	struct	st_data_s *st_gdata = tty->disc_data;
946	pr_info("%s ", __func__);
947
948	kfree_skb(st_gdata->tx_skb);
949	st_gdata->tx_skb = NULL;
950
951	tty->ops->flush_buffer(tty);
952	return;
953}
954
955/********************************************************************/
956int st_core_init(struct st_data_s **core_data)
957{
958	struct st_data_s *st_gdata;
959	long err;
960	static struct tty_ldisc_ops *st_ldisc_ops;
961
962	/* populate and register to TTY line discipline */
963	st_ldisc_ops = kzalloc(sizeof(*st_ldisc_ops), GFP_KERNEL);
964	if (!st_ldisc_ops) {
965		pr_err("no mem to allocate");
966		return -ENOMEM;
967	}
968
969	st_ldisc_ops->magic = TTY_LDISC_MAGIC;
970	st_ldisc_ops->name = "n_st";	/*"n_hci"; */
971	st_ldisc_ops->open = st_tty_open;
972	st_ldisc_ops->close = st_tty_close;
973	st_ldisc_ops->receive_buf = st_tty_receive;
974	st_ldisc_ops->write_wakeup = st_tty_wakeup;
975	st_ldisc_ops->flush_buffer = st_tty_flush_buffer;
976	st_ldisc_ops->owner = THIS_MODULE;
977
978	err = tty_register_ldisc(N_TI_WL, st_ldisc_ops);
979	if (err) {
980		pr_err("error registering %d line discipline %ld",
981			   N_TI_WL, err);
982		kfree(st_ldisc_ops);
983		return err;
984	}
985	pr_info("registered n_shared line discipline");
986
987	st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
988	if (!st_gdata) {
989		pr_err("memory allocation failed");
990		err = tty_unregister_ldisc(N_TI_WL);
991		if (err)
992			pr_err("unable to un-register ldisc %ld", err);
993		kfree(st_ldisc_ops);
994		err = -ENOMEM;
995		return err;
996	}
997
998	/* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
999	 * will be pushed in this queue for actual transmission.
1000	 */
1001	skb_queue_head_init(&st_gdata->txq);
1002	skb_queue_head_init(&st_gdata->tx_waitq);
1003
1004	/* Locking used in st_int_enqueue() to avoid multiple execution */
1005	spin_lock_init(&st_gdata->lock);
1006
1007	/* ldisc_ops ref to be only used in __exit of module */
1008	st_gdata->ldisc_ops = st_ldisc_ops;
1009
1010#if 0
1011	err = st_kim_init();
1012	if (err) {
1013		pr_err("error during kim initialization(%ld)", err);
1014		kfree(st_gdata);
1015		err = tty_unregister_ldisc(N_TI_WL);
1016		if (err)
1017			pr_err("unable to un-register ldisc");
1018		kfree(st_ldisc_ops);
1019		return -1;
1020	}
1021#endif
1022
1023	err = st_ll_init(st_gdata);
1024	if (err) {
1025		pr_err("error during st_ll initialization(%ld)", err);
1026		kfree(st_gdata);
1027		err = tty_unregister_ldisc(N_TI_WL);
1028		if (err)
1029			pr_err("unable to un-register ldisc");
1030		kfree(st_ldisc_ops);
1031		return -1;
1032	}
1033	*core_data = st_gdata;
1034	return 0;
1035}
1036
1037void st_core_exit(struct st_data_s *st_gdata)
1038{
1039	long err;
1040	/* internal module cleanup */
1041	err = st_ll_deinit(st_gdata);
1042	if (err)
1043		pr_err("error during deinit of ST LL %ld", err);
1044#if 0
1045	err = st_kim_deinit();
1046	if (err)
1047		pr_err("error during deinit of ST KIM %ld", err);
1048#endif
1049	if (st_gdata != NULL) {
1050		/* Free ST Tx Qs and skbs */
1051		skb_queue_purge(&st_gdata->txq);
1052		skb_queue_purge(&st_gdata->tx_waitq);
1053		kfree_skb(st_gdata->rx_skb);
1054		kfree_skb(st_gdata->tx_skb);
1055		/* TTY ldisc cleanup */
1056		err = tty_unregister_ldisc(N_TI_WL);
1057		if (err)
1058			pr_err("unable to un-register ldisc %ld", err);
1059		kfree(st_gdata->ldisc_ops);
1060		/* free the global data pointer */
1061		kfree(st_gdata);
1062	}
1063}
1064
1065
1066