xref: /openbmc/linux/drivers/misc/ti-st/st_core.c (revision 9c1f8594)
1 /*
2  *  Shared Transport Line discipline driver Core
3  *	This hooks up ST KIM driver and ST LL driver
4  *  Copyright (C) 2009-2010 Texas Instruments
5  *  Author: Pavan Savoy <pavan_savoy@ti.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 
22 #define pr_fmt(fmt)	"(stc): " fmt
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/tty.h>
27 
28 #include <linux/seq_file.h>
29 #include <linux/skbuff.h>
30 
31 #include <linux/ti_wilink_st.h>
32 
33 /* function pointer pointing to either,
34  * st_kim_recv during registration to receive fw download responses
35  * st_int_recv after registration to receive proto stack responses
36  */
37 void (*st_recv) (void*, const unsigned char*, long);
38 
39 /********************************************************************/
40 static void add_channel_to_table(struct st_data_s *st_gdata,
41 		struct st_proto_s *new_proto)
42 {
43 	pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
44 	/* list now has the channel id as index itself */
45 	st_gdata->list[new_proto->chnl_id] = new_proto;
46 	st_gdata->is_registered[new_proto->chnl_id] = true;
47 }
48 
49 static void remove_channel_from_table(struct st_data_s *st_gdata,
50 		struct st_proto_s *proto)
51 {
52 	pr_info("%s: id %d\n", __func__, proto->chnl_id);
53 /*	st_gdata->list[proto->chnl_id] = NULL; */
54 	st_gdata->is_registered[proto->chnl_id] = false;
55 }
56 
57 /*
58  * called from KIM during firmware download.
59  *
60  * This is a wrapper function to tty->ops->write_room.
61  * It returns number of free space available in
62  * uart tx buffer.
63  */
64 int st_get_uart_wr_room(struct st_data_s *st_gdata)
65 {
66 	struct tty_struct *tty;
67 	if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
68 		pr_err("tty unavailable to perform write");
69 		return -1;
70 	}
71 	tty = st_gdata->tty;
72 	return tty->ops->write_room(tty);
73 }
74 
75 /* can be called in from
76  * -- KIM (during fw download)
77  * -- ST Core (during st_write)
78  *
79  *  This is the internal write function - a wrapper
80  *  to tty->ops->write
81  */
82 int st_int_write(struct st_data_s *st_gdata,
83 	const unsigned char *data, int count)
84 {
85 	struct tty_struct *tty;
86 	if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
87 		pr_err("tty unavailable to perform write");
88 		return -EINVAL;
89 	}
90 	tty = st_gdata->tty;
91 #ifdef VERBOSE
92 	print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
93 		16, 1, data, count, 0);
94 #endif
95 	return tty->ops->write(tty, data, count);
96 
97 }
98 
99 /*
100  * push the skb received to relevant
101  * protocol stacks
102  */
103 void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
104 {
105 	pr_debug(" %s(prot:%d) ", __func__, chnl_id);
106 
107 	if (unlikely
108 	    (st_gdata == NULL || st_gdata->rx_skb == NULL
109 	     || st_gdata->is_registered[chnl_id] == false)) {
110 		pr_err("chnl_id %d not registered, no data to send?",
111 			   chnl_id);
112 		kfree_skb(st_gdata->rx_skb);
113 		return;
114 	}
115 	/* this cannot fail
116 	 * this shouldn't take long
117 	 * - should be just skb_queue_tail for the
118 	 *   protocol stack driver
119 	 */
120 	if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
121 		if (unlikely
122 			(st_gdata->list[chnl_id]->recv
123 			(st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
124 			     != 0)) {
125 			pr_err(" proto stack %d's ->recv failed", chnl_id);
126 			kfree_skb(st_gdata->rx_skb);
127 			return;
128 		}
129 	} else {
130 		pr_err(" proto stack %d's ->recv null", chnl_id);
131 		kfree_skb(st_gdata->rx_skb);
132 	}
133 	return;
134 }
135 
136 /**
137  * st_reg_complete -
138  * to call registration complete callbacks
139  * of all protocol stack drivers
140  */
141 void st_reg_complete(struct st_data_s *st_gdata, char err)
142 {
143 	unsigned char i = 0;
144 	pr_info(" %s ", __func__);
145 	for (i = 0; i < ST_MAX_CHANNELS; i++) {
146 		if (likely(st_gdata != NULL &&
147 			st_gdata->is_registered[i] == true &&
148 				st_gdata->list[i]->reg_complete_cb != NULL)) {
149 			st_gdata->list[i]->reg_complete_cb
150 				(st_gdata->list[i]->priv_data, err);
151 			pr_info("protocol %d's cb sent %d\n", i, err);
152 			if (err) { /* cleanup registered protocol */
153 				st_gdata->protos_registered--;
154 				st_gdata->is_registered[i] = false;
155 			}
156 		}
157 	}
158 }
159 
160 static inline int st_check_data_len(struct st_data_s *st_gdata,
161 	unsigned char chnl_id, int len)
162 {
163 	int room = skb_tailroom(st_gdata->rx_skb);
164 
165 	pr_debug("len %d room %d", len, room);
166 
167 	if (!len) {
168 		/* Received packet has only packet header and
169 		 * has zero length payload. So, ask ST CORE to
170 		 * forward the packet to protocol driver (BT/FM/GPS)
171 		 */
172 		st_send_frame(chnl_id, st_gdata);
173 
174 	} else if (len > room) {
175 		/* Received packet's payload length is larger.
176 		 * We can't accommodate it in created skb.
177 		 */
178 		pr_err("Data length is too large len %d room %d", len,
179 			   room);
180 		kfree_skb(st_gdata->rx_skb);
181 	} else {
182 		/* Packet header has non-zero payload length and
183 		 * we have enough space in created skb. Lets read
184 		 * payload data */
185 		st_gdata->rx_state = ST_W4_DATA;
186 		st_gdata->rx_count = len;
187 		return len;
188 	}
189 
190 	/* Change ST state to continue to process next
191 	 * packet */
192 	st_gdata->rx_state = ST_W4_PACKET_TYPE;
193 	st_gdata->rx_skb = NULL;
194 	st_gdata->rx_count = 0;
195 	st_gdata->rx_chnl = 0;
196 
197 	return 0;
198 }
199 
200 /**
201  * st_wakeup_ack - internal function for action when wake-up ack
202  *	received
203  */
204 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
205 	unsigned char cmd)
206 {
207 	struct sk_buff *waiting_skb;
208 	unsigned long flags = 0;
209 
210 	spin_lock_irqsave(&st_gdata->lock, flags);
211 	/* de-Q from waitQ and Q in txQ now that the
212 	 * chip is awake
213 	 */
214 	while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
215 		skb_queue_tail(&st_gdata->txq, waiting_skb);
216 
217 	/* state forwarded to ST LL */
218 	st_ll_sleep_state(st_gdata, (unsigned long)cmd);
219 	spin_unlock_irqrestore(&st_gdata->lock, flags);
220 
221 	/* wake up to send the recently copied skbs from waitQ */
222 	st_tx_wakeup(st_gdata);
223 }
224 
225 /**
226  * st_int_recv - ST's internal receive function.
227  *	Decodes received RAW data and forwards to corresponding
228  *	client drivers (Bluetooth,FM,GPS..etc).
229  *	This can receive various types of packets,
230  *	HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
231  *	CH-8 packets from FM, CH-9 packets from GPS cores.
232  */
233 void st_int_recv(void *disc_data,
234 	const unsigned char *data, long count)
235 {
236 	char *ptr;
237 	struct st_proto_s *proto;
238 	unsigned short payload_len = 0;
239 	int len = 0, type = 0;
240 	unsigned char *plen;
241 	struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
242 	unsigned long flags;
243 
244 	ptr = (char *)data;
245 	/* tty_receive sent null ? */
246 	if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
247 		pr_err(" received null from TTY ");
248 		return;
249 	}
250 
251 	pr_debug("count %ld rx_state %ld"
252 		   "rx_count %ld", count, st_gdata->rx_state,
253 		   st_gdata->rx_count);
254 
255 	spin_lock_irqsave(&st_gdata->lock, flags);
256 	/* Decode received bytes here */
257 	while (count) {
258 		if (st_gdata->rx_count) {
259 			len = min_t(unsigned int, st_gdata->rx_count, count);
260 			memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
261 			st_gdata->rx_count -= len;
262 			count -= len;
263 			ptr += len;
264 
265 			if (st_gdata->rx_count)
266 				continue;
267 
268 			/* Check ST RX state machine , where are we? */
269 			switch (st_gdata->rx_state) {
270 			/* Waiting for complete packet ? */
271 			case ST_W4_DATA:
272 				pr_debug("Complete pkt received");
273 				/* Ask ST CORE to forward
274 				 * the packet to protocol driver */
275 				st_send_frame(st_gdata->rx_chnl, st_gdata);
276 
277 				st_gdata->rx_state = ST_W4_PACKET_TYPE;
278 				st_gdata->rx_skb = NULL;
279 				continue;
280 			/* parse the header to know details */
281 			case ST_W4_HEADER:
282 				proto = st_gdata->list[st_gdata->rx_chnl];
283 				plen =
284 				&st_gdata->rx_skb->data
285 				[proto->offset_len_in_hdr];
286 				pr_debug("plen pointing to %x\n", *plen);
287 				if (proto->len_size == 1)/* 1 byte len field */
288 					payload_len = *(unsigned char *)plen;
289 				else if (proto->len_size == 2)
290 					payload_len =
291 					__le16_to_cpu(*(unsigned short *)plen);
292 				else
293 					pr_info("%s: invalid length "
294 					"for id %d\n",
295 					__func__, proto->chnl_id);
296 				st_check_data_len(st_gdata, proto->chnl_id,
297 						payload_len);
298 				pr_debug("off %d, pay len %d\n",
299 					proto->offset_len_in_hdr, payload_len);
300 				continue;
301 			}	/* end of switch rx_state */
302 		}
303 
304 		/* end of if rx_count */
305 		/* Check first byte of packet and identify module
306 		 * owner (BT/FM/GPS) */
307 		switch (*ptr) {
308 		case LL_SLEEP_IND:
309 		case LL_SLEEP_ACK:
310 		case LL_WAKE_UP_IND:
311 			pr_debug("PM packet");
312 			/* this takes appropriate action based on
313 			 * sleep state received --
314 			 */
315 			st_ll_sleep_state(st_gdata, *ptr);
316 			/* if WAKEUP_IND collides copy from waitq to txq
317 			 * and assume chip awake
318 			 */
319 			spin_unlock_irqrestore(&st_gdata->lock, flags);
320 			if (st_ll_getstate(st_gdata) == ST_LL_AWAKE)
321 				st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK);
322 			spin_lock_irqsave(&st_gdata->lock, flags);
323 
324 			ptr++;
325 			count--;
326 			continue;
327 		case LL_WAKE_UP_ACK:
328 			pr_debug("PM packet");
329 
330 			spin_unlock_irqrestore(&st_gdata->lock, flags);
331 			/* wake up ack received */
332 			st_wakeup_ack(st_gdata, *ptr);
333 			spin_lock_irqsave(&st_gdata->lock, flags);
334 
335 			ptr++;
336 			count--;
337 			continue;
338 			/* Unknow packet? */
339 		default:
340 			type = *ptr;
341 			if (st_gdata->list[type] == NULL) {
342 				pr_err("chip/interface misbehavior dropping"
343 					" frame starting with 0x%02x", type);
344 				goto done;
345 
346 			}
347 			st_gdata->rx_skb = alloc_skb(
348 					st_gdata->list[type]->max_frame_size,
349 					GFP_ATOMIC);
350 			skb_reserve(st_gdata->rx_skb,
351 					st_gdata->list[type]->reserve);
352 			/* next 2 required for BT only */
353 			st_gdata->rx_skb->cb[0] = type; /*pkt_type*/
354 			st_gdata->rx_skb->cb[1] = 0; /*incoming*/
355 			st_gdata->rx_chnl = *ptr;
356 			st_gdata->rx_state = ST_W4_HEADER;
357 			st_gdata->rx_count = st_gdata->list[type]->hdr_len;
358 			pr_debug("rx_count %ld\n", st_gdata->rx_count);
359 		};
360 		ptr++;
361 		count--;
362 	}
363 done:
364 	spin_unlock_irqrestore(&st_gdata->lock, flags);
365 	pr_debug("done %s", __func__);
366 	return;
367 }
368 
369 /**
370  * st_int_dequeue - internal de-Q function.
371  *	If the previous data set was not written
372  *	completely, return that skb which has the pending data.
373  *	In normal cases, return top of txq.
374  */
375 struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
376 {
377 	struct sk_buff *returning_skb;
378 
379 	pr_debug("%s", __func__);
380 	if (st_gdata->tx_skb != NULL) {
381 		returning_skb = st_gdata->tx_skb;
382 		st_gdata->tx_skb = NULL;
383 		return returning_skb;
384 	}
385 	return skb_dequeue(&st_gdata->txq);
386 }
387 
388 /**
389  * st_int_enqueue - internal Q-ing function.
390  *	Will either Q the skb to txq or the tx_waitq
391  *	depending on the ST LL state.
392  *	If the chip is asleep, then Q it onto waitq and
393  *	wakeup the chip.
394  *	txq and waitq needs protection since the other contexts
395  *	may be sending data, waking up chip.
396  */
397 void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
398 {
399 	unsigned long flags = 0;
400 
401 	pr_debug("%s", __func__);
402 	spin_lock_irqsave(&st_gdata->lock, flags);
403 
404 	switch (st_ll_getstate(st_gdata)) {
405 	case ST_LL_AWAKE:
406 		pr_debug("ST LL is AWAKE, sending normally");
407 		skb_queue_tail(&st_gdata->txq, skb);
408 		break;
409 	case ST_LL_ASLEEP_TO_AWAKE:
410 		skb_queue_tail(&st_gdata->tx_waitq, skb);
411 		break;
412 	case ST_LL_AWAKE_TO_ASLEEP:
413 		pr_err("ST LL is illegal state(%ld),"
414 			   "purging received skb.", st_ll_getstate(st_gdata));
415 		kfree_skb(skb);
416 		break;
417 	case ST_LL_ASLEEP:
418 		skb_queue_tail(&st_gdata->tx_waitq, skb);
419 		st_ll_wakeup(st_gdata);
420 		break;
421 	default:
422 		pr_err("ST LL is illegal state(%ld),"
423 			   "purging received skb.", st_ll_getstate(st_gdata));
424 		kfree_skb(skb);
425 		break;
426 	}
427 
428 	spin_unlock_irqrestore(&st_gdata->lock, flags);
429 	pr_debug("done %s", __func__);
430 	return;
431 }
432 
433 /*
434  * internal wakeup function
435  * called from either
436  * - TTY layer when write's finished
437  * - st_write (in context of the protocol stack)
438  */
439 void st_tx_wakeup(struct st_data_s *st_data)
440 {
441 	struct sk_buff *skb;
442 	unsigned long flags;	/* for irq save flags */
443 	pr_debug("%s", __func__);
444 	/* check for sending & set flag sending here */
445 	if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
446 		pr_debug("ST already sending");
447 		/* keep sending */
448 		set_bit(ST_TX_WAKEUP, &st_data->tx_state);
449 		return;
450 		/* TX_WAKEUP will be checked in another
451 		 * context
452 		 */
453 	}
454 	do {			/* come back if st_tx_wakeup is set */
455 		/* woke-up to write */
456 		clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
457 		while ((skb = st_int_dequeue(st_data))) {
458 			int len;
459 			spin_lock_irqsave(&st_data->lock, flags);
460 			/* enable wake-up from TTY */
461 			set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
462 			len = st_int_write(st_data, skb->data, skb->len);
463 			skb_pull(skb, len);
464 			/* if skb->len = len as expected, skb->len=0 */
465 			if (skb->len) {
466 				/* would be the next skb to be sent */
467 				st_data->tx_skb = skb;
468 				spin_unlock_irqrestore(&st_data->lock, flags);
469 				break;
470 			}
471 			kfree_skb(skb);
472 			spin_unlock_irqrestore(&st_data->lock, flags);
473 		}
474 		/* if wake-up is set in another context- restart sending */
475 	} while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
476 
477 	/* clear flag sending */
478 	clear_bit(ST_TX_SENDING, &st_data->tx_state);
479 }
480 
481 /********************************************************************/
482 /* functions called from ST KIM
483 */
484 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
485 {
486 	seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
487 			st_gdata->protos_registered,
488 			st_gdata->is_registered[0x04] == true ? 'R' : 'U',
489 			st_gdata->is_registered[0x08] == true ? 'R' : 'U',
490 			st_gdata->is_registered[0x09] == true ? 'R' : 'U');
491 }
492 
493 /********************************************************************/
494 /*
495  * functions called from protocol stack drivers
496  * to be EXPORT-ed
497  */
498 long st_register(struct st_proto_s *new_proto)
499 {
500 	struct st_data_s	*st_gdata;
501 	long err = 0;
502 	unsigned long flags = 0;
503 
504 	st_kim_ref(&st_gdata, 0);
505 	pr_info("%s(%d) ", __func__, new_proto->chnl_id);
506 	if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
507 	    || new_proto->reg_complete_cb == NULL) {
508 		pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
509 		return -EINVAL;
510 	}
511 
512 	if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
513 		pr_err("chnl_id %d not supported", new_proto->chnl_id);
514 		return -EPROTONOSUPPORT;
515 	}
516 
517 	if (st_gdata->is_registered[new_proto->chnl_id] == true) {
518 		pr_err("chnl_id %d already registered", new_proto->chnl_id);
519 		return -EALREADY;
520 	}
521 
522 	/* can be from process context only */
523 	spin_lock_irqsave(&st_gdata->lock, flags);
524 
525 	if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
526 		pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
527 		/* fw download in progress */
528 
529 		add_channel_to_table(st_gdata, new_proto);
530 		st_gdata->protos_registered++;
531 		new_proto->write = st_write;
532 
533 		set_bit(ST_REG_PENDING, &st_gdata->st_state);
534 		spin_unlock_irqrestore(&st_gdata->lock, flags);
535 		return -EINPROGRESS;
536 	} else if (st_gdata->protos_registered == ST_EMPTY) {
537 		pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
538 		set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
539 		st_recv = st_kim_recv;
540 
541 		/* release lock previously held - re-locked below */
542 		spin_unlock_irqrestore(&st_gdata->lock, flags);
543 
544 		/* enable the ST LL - to set default chip state */
545 		st_ll_enable(st_gdata);
546 		/* this may take a while to complete
547 		 * since it involves BT fw download
548 		 */
549 		err = st_kim_start(st_gdata->kim_data);
550 		if (err != 0) {
551 			clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
552 			if ((st_gdata->protos_registered != ST_EMPTY) &&
553 			    (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
554 				pr_err(" KIM failure complete callback ");
555 				st_reg_complete(st_gdata, err);
556 			}
557 			return -EINVAL;
558 		}
559 
560 		clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
561 		st_recv = st_int_recv;
562 
563 		/* this is where all pending registration
564 		 * are signalled to be complete by calling callback functions
565 		 */
566 		if ((st_gdata->protos_registered != ST_EMPTY) &&
567 		    (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
568 			pr_debug(" call reg complete callback ");
569 			st_reg_complete(st_gdata, 0);
570 		}
571 		clear_bit(ST_REG_PENDING, &st_gdata->st_state);
572 
573 		/* check for already registered once more,
574 		 * since the above check is old
575 		 */
576 		if (st_gdata->is_registered[new_proto->chnl_id] == true) {
577 			pr_err(" proto %d already registered ",
578 				   new_proto->chnl_id);
579 			return -EALREADY;
580 		}
581 
582 		spin_lock_irqsave(&st_gdata->lock, flags);
583 		add_channel_to_table(st_gdata, new_proto);
584 		st_gdata->protos_registered++;
585 		new_proto->write = st_write;
586 		spin_unlock_irqrestore(&st_gdata->lock, flags);
587 		return err;
588 	}
589 	/* if fw is already downloaded & new stack registers protocol */
590 	else {
591 		add_channel_to_table(st_gdata, new_proto);
592 		st_gdata->protos_registered++;
593 		new_proto->write = st_write;
594 
595 		/* lock already held before entering else */
596 		spin_unlock_irqrestore(&st_gdata->lock, flags);
597 		return err;
598 	}
599 	pr_debug("done %s(%d) ", __func__, new_proto->chnl_id);
600 }
601 EXPORT_SYMBOL_GPL(st_register);
602 
603 /* to unregister a protocol -
604  * to be called from protocol stack driver
605  */
606 long st_unregister(struct st_proto_s *proto)
607 {
608 	long err = 0;
609 	unsigned long flags = 0;
610 	struct st_data_s	*st_gdata;
611 
612 	pr_debug("%s: %d ", __func__, proto->chnl_id);
613 
614 	st_kim_ref(&st_gdata, 0);
615 	if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
616 		pr_err(" chnl_id %d not supported", proto->chnl_id);
617 		return -EPROTONOSUPPORT;
618 	}
619 
620 	spin_lock_irqsave(&st_gdata->lock, flags);
621 
622 	if (st_gdata->list[proto->chnl_id] == NULL) {
623 		pr_err(" chnl_id %d not registered", proto->chnl_id);
624 		spin_unlock_irqrestore(&st_gdata->lock, flags);
625 		return -EPROTONOSUPPORT;
626 	}
627 
628 	st_gdata->protos_registered--;
629 	remove_channel_from_table(st_gdata, proto);
630 	spin_unlock_irqrestore(&st_gdata->lock, flags);
631 
632 	if ((st_gdata->protos_registered == ST_EMPTY) &&
633 	    (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
634 		pr_info(" all chnl_ids unregistered ");
635 
636 		/* stop traffic on tty */
637 		if (st_gdata->tty) {
638 			tty_ldisc_flush(st_gdata->tty);
639 			stop_tty(st_gdata->tty);
640 		}
641 
642 		/* all chnl_ids now unregistered */
643 		st_kim_stop(st_gdata->kim_data);
644 		/* disable ST LL */
645 		st_ll_disable(st_gdata);
646 	}
647 	return err;
648 }
649 
650 /*
651  * called in protocol stack drivers
652  * via the write function pointer
653  */
654 long st_write(struct sk_buff *skb)
655 {
656 	struct st_data_s *st_gdata;
657 	long len;
658 
659 	st_kim_ref(&st_gdata, 0);
660 	if (unlikely(skb == NULL || st_gdata == NULL
661 		|| st_gdata->tty == NULL)) {
662 		pr_err("data/tty unavailable to perform write");
663 		return -EINVAL;
664 	}
665 
666 	pr_debug("%d to be written", skb->len);
667 	len = skb->len;
668 
669 	/* st_ll to decide where to enqueue the skb */
670 	st_int_enqueue(st_gdata, skb);
671 	/* wake up */
672 	st_tx_wakeup(st_gdata);
673 
674 	/* return number of bytes written */
675 	return len;
676 }
677 
678 /* for protocols making use of shared transport */
679 EXPORT_SYMBOL_GPL(st_unregister);
680 
681 /********************************************************************/
682 /*
683  * functions called from TTY layer
684  */
685 static int st_tty_open(struct tty_struct *tty)
686 {
687 	int err = 0;
688 	struct st_data_s *st_gdata;
689 	pr_info("%s ", __func__);
690 
691 	st_kim_ref(&st_gdata, 0);
692 	st_gdata->tty = tty;
693 	tty->disc_data = st_gdata;
694 
695 	/* don't do an wakeup for now */
696 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
697 
698 	/* mem already allocated
699 	 */
700 	tty->receive_room = 65536;
701 	/* Flush any pending characters in the driver and discipline. */
702 	tty_ldisc_flush(tty);
703 	tty_driver_flush_buffer(tty);
704 	/*
705 	 * signal to UIM via KIM that -
706 	 * installation of N_TI_WL ldisc is complete
707 	 */
708 	st_kim_complete(st_gdata->kim_data);
709 	pr_debug("done %s", __func__);
710 	return err;
711 }
712 
713 static void st_tty_close(struct tty_struct *tty)
714 {
715 	unsigned char i = ST_MAX_CHANNELS;
716 	unsigned long flags = 0;
717 	struct	st_data_s *st_gdata = tty->disc_data;
718 
719 	pr_info("%s ", __func__);
720 
721 	/* TODO:
722 	 * if a protocol has been registered & line discipline
723 	 * un-installed for some reason - what should be done ?
724 	 */
725 	spin_lock_irqsave(&st_gdata->lock, flags);
726 	for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
727 		if (st_gdata->is_registered[i] == true)
728 			pr_err("%d not un-registered", i);
729 		st_gdata->list[i] = NULL;
730 		st_gdata->is_registered[i] = false;
731 	}
732 	st_gdata->protos_registered = 0;
733 	spin_unlock_irqrestore(&st_gdata->lock, flags);
734 	/*
735 	 * signal to UIM via KIM that -
736 	 * N_TI_WL ldisc is un-installed
737 	 */
738 	st_kim_complete(st_gdata->kim_data);
739 	st_gdata->tty = NULL;
740 	/* Flush any pending characters in the driver and discipline. */
741 	tty_ldisc_flush(tty);
742 	tty_driver_flush_buffer(tty);
743 
744 	spin_lock_irqsave(&st_gdata->lock, flags);
745 	/* empty out txq and tx_waitq */
746 	skb_queue_purge(&st_gdata->txq);
747 	skb_queue_purge(&st_gdata->tx_waitq);
748 	/* reset the TTY Rx states of ST */
749 	st_gdata->rx_count = 0;
750 	st_gdata->rx_state = ST_W4_PACKET_TYPE;
751 	kfree_skb(st_gdata->rx_skb);
752 	st_gdata->rx_skb = NULL;
753 	spin_unlock_irqrestore(&st_gdata->lock, flags);
754 
755 	pr_debug("%s: done ", __func__);
756 }
757 
758 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
759 			   char *tty_flags, int count)
760 {
761 #ifdef VERBOSE
762 	print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
763 		16, 1, data, count, 0);
764 #endif
765 
766 	/*
767 	 * if fw download is in progress then route incoming data
768 	 * to KIM for validation
769 	 */
770 	st_recv(tty->disc_data, data, count);
771 	pr_debug("done %s", __func__);
772 }
773 
774 /* wake-up function called in from the TTY layer
775  * inside the internal wakeup function will be called
776  */
777 static void st_tty_wakeup(struct tty_struct *tty)
778 {
779 	struct	st_data_s *st_gdata = tty->disc_data;
780 	pr_debug("%s ", __func__);
781 	/* don't do an wakeup for now */
782 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
783 
784 	/* call our internal wakeup */
785 	st_tx_wakeup((void *)st_gdata);
786 }
787 
788 static void st_tty_flush_buffer(struct tty_struct *tty)
789 {
790 	struct	st_data_s *st_gdata = tty->disc_data;
791 	pr_debug("%s ", __func__);
792 
793 	kfree_skb(st_gdata->tx_skb);
794 	st_gdata->tx_skb = NULL;
795 
796 	tty->ops->flush_buffer(tty);
797 	return;
798 }
799 
800 static struct tty_ldisc_ops st_ldisc_ops = {
801 	.magic = TTY_LDISC_MAGIC,
802 	.name = "n_st",
803 	.open = st_tty_open,
804 	.close = st_tty_close,
805 	.receive_buf = st_tty_receive,
806 	.write_wakeup = st_tty_wakeup,
807 	.flush_buffer = st_tty_flush_buffer,
808 	.owner = THIS_MODULE
809 };
810 
811 /********************************************************************/
812 int st_core_init(struct st_data_s **core_data)
813 {
814 	struct st_data_s *st_gdata;
815 	long err;
816 
817 	err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops);
818 	if (err) {
819 		pr_err("error registering %d line discipline %ld",
820 			   N_TI_WL, err);
821 		return err;
822 	}
823 	pr_debug("registered n_shared line discipline");
824 
825 	st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
826 	if (!st_gdata) {
827 		pr_err("memory allocation failed");
828 		err = tty_unregister_ldisc(N_TI_WL);
829 		if (err)
830 			pr_err("unable to un-register ldisc %ld", err);
831 		err = -ENOMEM;
832 		return err;
833 	}
834 
835 	/* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
836 	 * will be pushed in this queue for actual transmission.
837 	 */
838 	skb_queue_head_init(&st_gdata->txq);
839 	skb_queue_head_init(&st_gdata->tx_waitq);
840 
841 	/* Locking used in st_int_enqueue() to avoid multiple execution */
842 	spin_lock_init(&st_gdata->lock);
843 
844 	err = st_ll_init(st_gdata);
845 	if (err) {
846 		pr_err("error during st_ll initialization(%ld)", err);
847 		kfree(st_gdata);
848 		err = tty_unregister_ldisc(N_TI_WL);
849 		if (err)
850 			pr_err("unable to un-register ldisc");
851 		return err;
852 	}
853 	*core_data = st_gdata;
854 	return 0;
855 }
856 
857 void st_core_exit(struct st_data_s *st_gdata)
858 {
859 	long err;
860 	/* internal module cleanup */
861 	err = st_ll_deinit(st_gdata);
862 	if (err)
863 		pr_err("error during deinit of ST LL %ld", err);
864 
865 	if (st_gdata != NULL) {
866 		/* Free ST Tx Qs and skbs */
867 		skb_queue_purge(&st_gdata->txq);
868 		skb_queue_purge(&st_gdata->tx_waitq);
869 		kfree_skb(st_gdata->rx_skb);
870 		kfree_skb(st_gdata->tx_skb);
871 		/* TTY ldisc cleanup */
872 		err = tty_unregister_ldisc(N_TI_WL);
873 		if (err)
874 			pr_err("unable to un-register ldisc %ld", err);
875 		/* free the global data pointer */
876 		kfree(st_gdata);
877 	}
878 }
879 
880 
881