2 * Shared Transport Line discipline driver Core
3 * This hooks up ST KIM driver and ST LL driver
4 * Copyright (C) 2009 Texas Instruments
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.
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.
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
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>
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>
33 * packet formats for fm and gps
41 /* strings to be used for rfkill entries and by
42 * ST Core to be used for sysfs debug entry
44 #define PROTO_ENTRY(type, name) name
45 const unsigned char *protocol_strngs[] = {
46 PROTO_ENTRY(ST_BT, "Bluetooth"),
47 PROTO_ENTRY(ST_FM, "FM"),
48 PROTO_ENTRY(ST_GPS, "GPS"),
50 /* function pointer pointing to either,
51 * st_kim_recv during registration to receive fw download responses
52 * st_int_recv after registration to receive proto stack responses
54 void (*st_recv) (void*, const unsigned char*, long);
56 /********************************************************************/
58 /* internal misc functions */
59 bool is_protocol_list_empty(void)
62 pr_debug(" %s ", __func__);
63 for (i = 0; i < ST_MAX; i++) {
64 if (st_gdata->list[i] != NULL)
73 /* can be called in from
74 * -- KIM (during fw download)
75 * -- ST Core (during st_write)
77 * This is the internal write function - a wrapper
80 int st_int_write(struct st_data_s *st_gdata,
81 const unsigned char *data, int count)
83 struct tty_struct *tty;
84 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
85 pr_err("tty unavailable to perform write");
90 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
91 16, 1, data, count, 0);
93 return tty->ops->write(tty, data, count);
98 * push the skb received to relevant
101 void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
103 pr_info(" %s(prot:%d) ", __func__, protoid);
106 (st_gdata == NULL || st_gdata->rx_skb == NULL
107 || st_gdata->list[protoid] == NULL)) {
108 pr_err("protocol %d not registered, no data to send?",
110 kfree_skb(st_gdata->rx_skb);
114 * this shouldn't take long
115 * - should be just skb_queue_tail for the
116 * protocol stack driver
118 if (likely(st_gdata->list[protoid]->recv != NULL)) {
120 (st_gdata->list[protoid]->recv
121 (st_gdata->list[protoid]->priv_data, st_gdata->rx_skb)
123 pr_err(" proto stack %d's ->recv failed", protoid);
124 kfree_skb(st_gdata->rx_skb);
128 pr_err(" proto stack %d's ->recv null", protoid);
129 kfree_skb(st_gdata->rx_skb);
136 * to call registration complete callbacks
137 * of all protocol stack drivers
139 void st_reg_complete(struct st_data_s *st_gdata, char err)
142 pr_info(" %s ", __func__);
143 for (i = 0; i < ST_MAX; i++) {
144 if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
145 st_gdata->list[i]->reg_complete_cb != NULL))
146 st_gdata->list[i]->reg_complete_cb
147 (st_gdata->list[i]->priv_data, err);
151 static inline int st_check_data_len(struct st_data_s *st_gdata,
152 int protoid, int len)
154 register int room = skb_tailroom(st_gdata->rx_skb);
156 pr_debug("len %d room %d", len, room);
159 /* Received packet has only packet header and
160 * has zero length payload. So, ask ST CORE to
161 * forward the packet to protocol driver (BT/FM/GPS)
163 st_send_frame(protoid, st_gdata);
165 } else if (len > room) {
166 /* Received packet's payload length is larger.
167 * We can't accommodate it in created skb.
169 pr_err("Data length is too large len %d room %d", len,
171 kfree_skb(st_gdata->rx_skb);
173 /* Packet header has non-zero payload length and
174 * we have enough space in created skb. Lets read
176 st_gdata->rx_state = ST_BT_W4_DATA;
177 st_gdata->rx_count = len;
181 /* Change ST state to continue to process next
183 st_gdata->rx_state = ST_W4_PACKET_TYPE;
184 st_gdata->rx_skb = NULL;
185 st_gdata->rx_count = 0;
191 * st_wakeup_ack - internal function for action when wake-up ack
194 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
197 register struct sk_buff *waiting_skb;
198 unsigned long flags = 0;
200 spin_lock_irqsave(&st_gdata->lock, flags);
201 /* de-Q from waitQ and Q in txQ now that the
204 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
205 skb_queue_tail(&st_gdata->txq, waiting_skb);
207 /* state forwarded to ST LL */
208 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
209 spin_unlock_irqrestore(&st_gdata->lock, flags);
211 /* wake up to send the recently copied skbs from waitQ */
212 st_tx_wakeup(st_gdata);
216 * st_int_recv - ST's internal receive function.
217 * Decodes received RAW data and forwards to corresponding
218 * client drivers (Bluetooth,FM,GPS..etc).
219 * This can receive various types of packets,
220 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
221 * CH-8 packets from FM, CH-9 packets from GPS cores.
223 void st_int_recv(void *disc_data,
224 const unsigned char *data, long count)
227 struct hci_event_hdr *eh;
228 struct hci_acl_hdr *ah;
229 struct hci_sco_hdr *sh;
230 struct fm_event_hdr *fm;
231 struct gps_event_hdr *gps;
232 register int len = 0, type = 0, dlen = 0;
233 static enum proto_type protoid = ST_MAX;
234 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
237 /* tty_receive sent null ? */
238 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
239 pr_err(" received null from TTY ");
243 pr_info("count %ld rx_state %ld"
244 "rx_count %ld", count, st_gdata->rx_state,
247 /* Decode received bytes here */
249 if (st_gdata->rx_count) {
250 len = min_t(unsigned int, st_gdata->rx_count, count);
251 memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
252 st_gdata->rx_count -= len;
256 if (st_gdata->rx_count)
259 /* Check ST RX state machine , where are we? */
260 switch (st_gdata->rx_state) {
262 /* Waiting for complete packet ? */
264 pr_debug("Complete pkt received");
266 /* Ask ST CORE to forward
267 * the packet to protocol driver */
268 st_send_frame(protoid, st_gdata);
270 st_gdata->rx_state = ST_W4_PACKET_TYPE;
271 st_gdata->rx_skb = NULL;
272 protoid = ST_MAX; /* is this required ? */
275 /* Waiting for Bluetooth event header ? */
276 case ST_BT_W4_EVENT_HDR:
277 eh = (struct hci_event_hdr *)st_gdata->rx_skb->
280 pr_debug("Event header: evt 0x%2.2x"
281 "plen %d", eh->evt, eh->plen);
283 st_check_data_len(st_gdata, protoid, eh->plen);
286 /* Waiting for Bluetooth acl header ? */
287 case ST_BT_W4_ACL_HDR:
288 ah = (struct hci_acl_hdr *)st_gdata->rx_skb->
290 dlen = __le16_to_cpu(ah->dlen);
292 pr_info("ACL header: dlen %d", dlen);
294 st_check_data_len(st_gdata, protoid, dlen);
297 /* Waiting for Bluetooth sco header ? */
298 case ST_BT_W4_SCO_HDR:
299 sh = (struct hci_sco_hdr *)st_gdata->rx_skb->
302 pr_info("SCO header: dlen %d", sh->dlen);
304 st_check_data_len(st_gdata, protoid, sh->dlen);
306 case ST_FM_W4_EVENT_HDR:
307 fm = (struct fm_event_hdr *)st_gdata->rx_skb->
309 pr_info("FM Header: ");
310 st_check_data_len(st_gdata, ST_FM, fm->plen);
312 /* TODO : Add GPS packet machine logic here */
313 case ST_GPS_W4_EVENT_HDR:
314 /* [0x09 pkt hdr][R/W byte][2 byte len] */
315 gps = (struct gps_event_hdr *)st_gdata->rx_skb->
317 pr_info("GPS Header: ");
318 st_check_data_len(st_gdata, ST_GPS, gps->plen);
320 } /* end of switch rx_state */
323 /* end of if rx_count */
324 /* Check first byte of packet and identify module
325 * owner (BT/FM/GPS) */
328 /* Bluetooth event packet? */
330 pr_info("Event packet");
331 st_gdata->rx_state = ST_BT_W4_EVENT_HDR;
332 st_gdata->rx_count = HCI_EVENT_HDR_SIZE;
333 type = HCI_EVENT_PKT;
337 /* Bluetooth acl packet? */
338 case HCI_ACLDATA_PKT:
339 pr_info("ACL packet");
340 st_gdata->rx_state = ST_BT_W4_ACL_HDR;
341 st_gdata->rx_count = HCI_ACL_HDR_SIZE;
342 type = HCI_ACLDATA_PKT;
346 /* Bluetooth sco packet? */
347 case HCI_SCODATA_PKT:
348 pr_info("SCO packet");
349 st_gdata->rx_state = ST_BT_W4_SCO_HDR;
350 st_gdata->rx_count = HCI_SCO_HDR_SIZE;
351 type = HCI_SCODATA_PKT;
355 /* Channel 8(FM) packet? */
357 pr_info("FM CH8 packet");
358 type = ST_FM_CH8_PKT;
359 st_gdata->rx_state = ST_FM_W4_EVENT_HDR;
360 st_gdata->rx_count = FM_EVENT_HDR_SIZE;
364 /* Channel 9(GPS) packet? */
365 case 0x9: /*ST_LL_GPS_CH9_PKT */
366 pr_info("GPS CH9 packet");
367 type = 0x9; /* ST_LL_GPS_CH9_PKT; */
369 st_gdata->rx_state = ST_GPS_W4_EVENT_HDR;
370 st_gdata->rx_count = 3; /* GPS_EVENT_HDR_SIZE -1*/
375 pr_info("PM packet");
376 /* this takes appropriate action based on
377 * sleep state received --
379 st_ll_sleep_state(st_gdata, *ptr);
384 pr_info("PM packet");
385 /* wake up ack received */
386 st_wakeup_ack(st_gdata, *ptr);
392 pr_err("Unknown packet type %2.2x", (__u8) *ptr);
402 /* Allocate new packet to hold received data */
404 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
405 if (!st_gdata->rx_skb) {
406 pr_err("Can't allocate mem for new packet");
407 st_gdata->rx_state = ST_W4_PACKET_TYPE;
408 st_gdata->rx_count = 0;
411 bt_cb(st_gdata->rx_skb)->pkt_type = type;
413 case ST_FM: /* for FM */
415 alloc_skb(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
416 if (!st_gdata->rx_skb) {
417 pr_err("Can't allocate mem for new packet");
418 st_gdata->rx_state = ST_W4_PACKET_TYPE;
419 st_gdata->rx_count = 0;
422 /* place holder 0x08 */
423 skb_reserve(st_gdata->rx_skb, 1);
424 st_gdata->rx_skb->cb[0] = ST_FM_CH8_PKT;
429 alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC);
430 if (!st_gdata->rx_skb) {
431 pr_err("Can't allocate mem for new packet");
432 st_gdata->rx_state = ST_W4_PACKET_TYPE;
433 st_gdata->rx_count = 0;
436 /* place holder 0x09 */
437 skb_reserve(st_gdata->rx_skb, 1);
438 st_gdata->rx_skb->cb[0] = 0x09; /*ST_GPS_CH9_PKT; */
444 pr_debug("done %s", __func__);
449 * st_int_dequeue - internal de-Q function.
450 * If the previous data set was not written
451 * completely, return that skb which has the pending data.
452 * In normal cases, return top of txq.
454 struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
456 struct sk_buff *returning_skb;
458 pr_debug("%s", __func__);
459 if (st_gdata->tx_skb != NULL) {
460 returning_skb = st_gdata->tx_skb;
461 st_gdata->tx_skb = NULL;
462 return returning_skb;
464 return skb_dequeue(&st_gdata->txq);
468 * st_int_enqueue - internal Q-ing function.
469 * Will either Q the skb to txq or the tx_waitq
470 * depending on the ST LL state.
471 * If the chip is asleep, then Q it onto waitq and
473 * txq and waitq needs protection since the other contexts
474 * may be sending data, waking up chip.
476 void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
478 unsigned long flags = 0;
480 pr_debug("%s", __func__);
481 spin_lock_irqsave(&st_gdata->lock, flags);
483 switch (st_ll_getstate(st_gdata)) {
485 pr_info("ST LL is AWAKE, sending normally");
486 skb_queue_tail(&st_gdata->txq, skb);
488 case ST_LL_ASLEEP_TO_AWAKE:
489 skb_queue_tail(&st_gdata->tx_waitq, skb);
491 case ST_LL_AWAKE_TO_ASLEEP:
492 pr_err("ST LL is illegal state(%ld),"
493 "purging received skb.", st_ll_getstate(st_gdata));
497 skb_queue_tail(&st_gdata->tx_waitq, skb);
498 st_ll_wakeup(st_gdata);
501 pr_err("ST LL is illegal state(%ld),"
502 "purging received skb.", st_ll_getstate(st_gdata));
507 spin_unlock_irqrestore(&st_gdata->lock, flags);
508 pr_debug("done %s", __func__);
513 * internal wakeup function
515 * - TTY layer when write's finished
516 * - st_write (in context of the protocol stack)
518 void st_tx_wakeup(struct st_data_s *st_data)
521 unsigned long flags; /* for irq save flags */
522 pr_debug("%s", __func__);
523 /* check for sending & set flag sending here */
524 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
525 pr_info("ST already sending");
527 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
529 /* TX_WAKEUP will be checked in another
533 do { /* come back if st_tx_wakeup is set */
534 /* woke-up to write */
535 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
536 while ((skb = st_int_dequeue(st_data))) {
538 spin_lock_irqsave(&st_data->lock, flags);
539 /* enable wake-up from TTY */
540 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
541 len = st_int_write(st_data, skb->data, skb->len);
543 /* if skb->len = len as expected, skb->len=0 */
545 /* would be the next skb to be sent */
546 st_data->tx_skb = skb;
547 spin_unlock_irqrestore(&st_data->lock, flags);
551 spin_unlock_irqrestore(&st_data->lock, flags);
553 /* if wake-up is set in another context- restart sending */
554 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
556 /* clear flag sending */
557 clear_bit(ST_TX_SENDING, &st_data->tx_state);
560 /********************************************************************/
561 /* functions called from ST KIM
563 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
565 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
566 st_gdata->protos_registered,
567 st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
568 st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
569 st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
572 /********************************************************************/
574 * functions called from protocol stack drivers
577 long st_register(struct st_proto_s *new_proto)
579 struct st_data_s *st_gdata;
581 unsigned long flags = 0;
583 st_kim_ref(&st_gdata, 0);
584 pr_info("%s(%d) ", __func__, new_proto->type);
585 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
586 || new_proto->reg_complete_cb == NULL) {
587 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
591 if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
592 pr_err("protocol %d not supported", new_proto->type);
593 return -EPROTONOSUPPORT;
596 if (st_gdata->list[new_proto->type] != NULL) {
597 pr_err("protocol %d already registered", new_proto->type);
601 /* can be from process context only */
602 spin_lock_irqsave(&st_gdata->lock, flags);
604 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
605 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
606 /* fw download in progress */
607 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
609 st_gdata->list[new_proto->type] = new_proto;
610 st_gdata->protos_registered++;
611 new_proto->write = st_write;
613 set_bit(ST_REG_PENDING, &st_gdata->st_state);
614 spin_unlock_irqrestore(&st_gdata->lock, flags);
616 } else if (st_gdata->protos_registered == ST_EMPTY) {
617 pr_info(" protocol list empty :%d ", new_proto->type);
618 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
619 st_recv = st_kim_recv;
621 /* release lock previously held - re-locked below */
622 spin_unlock_irqrestore(&st_gdata->lock, flags);
624 /* enable the ST LL - to set default chip state */
625 st_ll_enable(st_gdata);
626 /* this may take a while to complete
627 * since it involves BT fw download
629 err = st_kim_start(st_gdata->kim_data);
631 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
632 if ((st_gdata->protos_registered != ST_EMPTY) &&
633 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
634 pr_err(" KIM failure complete callback ");
635 st_reg_complete(st_gdata, -1);
641 /* the protocol might require other gpios to be toggled
643 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
645 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
646 st_recv = st_int_recv;
648 /* this is where all pending registration
649 * are signalled to be complete by calling callback functions
651 if ((st_gdata->protos_registered != ST_EMPTY) &&
652 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
653 pr_debug(" call reg complete callback ");
654 st_reg_complete(st_gdata, 0);
656 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
658 /* check for already registered once more,
659 * since the above check is old
661 if (st_gdata->list[new_proto->type] != NULL) {
662 pr_err(" proto %d already registered ",
667 spin_lock_irqsave(&st_gdata->lock, flags);
668 st_gdata->list[new_proto->type] = new_proto;
669 st_gdata->protos_registered++;
670 new_proto->write = st_write;
671 spin_unlock_irqrestore(&st_gdata->lock, flags);
674 /* if fw is already downloaded & new stack registers protocol */
676 switch (new_proto->type) {
682 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
686 pr_err("%d protocol not supported",
688 err = -EPROTONOSUPPORT;
689 /* something wrong */
692 st_gdata->list[new_proto->type] = new_proto;
693 st_gdata->protos_registered++;
694 new_proto->write = st_write;
696 /* lock already held before entering else */
697 spin_unlock_irqrestore(&st_gdata->lock, flags);
700 pr_debug("done %s(%d) ", __func__, new_proto->type);
702 EXPORT_SYMBOL_GPL(st_register);
704 /* to unregister a protocol -
705 * to be called from protocol stack driver
707 long st_unregister(enum proto_type type)
710 unsigned long flags = 0;
711 struct st_data_s *st_gdata;
713 pr_debug("%s: %d ", __func__, type);
715 st_kim_ref(&st_gdata, 0);
716 if (type < ST_BT || type >= ST_MAX) {
717 pr_err(" protocol %d not supported", type);
718 return -EPROTONOSUPPORT;
721 spin_lock_irqsave(&st_gdata->lock, flags);
723 if (st_gdata->list[type] == NULL) {
724 pr_err(" protocol %d not registered", type);
725 spin_unlock_irqrestore(&st_gdata->lock, flags);
726 return -EPROTONOSUPPORT;
729 st_gdata->protos_registered--;
730 st_gdata->list[type] = NULL;
732 /* kim ignores BT in the below function
733 * and handles the rest, BT is toggled
734 * only in kim_start and kim_stop
736 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
737 spin_unlock_irqrestore(&st_gdata->lock, flags);
739 if ((st_gdata->protos_registered == ST_EMPTY) &&
740 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
741 pr_info(" all protocols unregistered ");
743 /* stop traffic on tty */
745 tty_ldisc_flush(st_gdata->tty);
746 stop_tty(st_gdata->tty);
749 /* all protocols now unregistered */
750 st_kim_stop(st_gdata->kim_data);
752 st_ll_disable(st_gdata);
758 * called in protocol stack drivers
759 * via the write function pointer
761 long st_write(struct sk_buff *skb)
763 struct st_data_s *st_gdata;
765 enum proto_type protoid = ST_MAX;
769 st_kim_ref(&st_gdata, 0);
770 if (unlikely(skb == NULL || st_gdata == NULL
771 || st_gdata->tty == NULL)) {
772 pr_err("data/tty unavailable to perform write");
775 #ifdef DEBUG /* open-up skb to read the 1st byte */
776 switch (skb->data[0]) {
777 case HCI_COMMAND_PKT:
778 case HCI_ACLDATA_PKT:
779 case HCI_SCODATA_PKT:
789 if (unlikely(st_gdata->list[protoid] == NULL)) {
790 pr_err(" protocol %d not registered, and writing? ",
795 pr_debug("%d to be written", skb->len);
798 /* st_ll to decide where to enqueue the skb */
799 st_int_enqueue(st_gdata, skb);
801 st_tx_wakeup(st_gdata);
803 /* return number of bytes written */
807 /* for protocols making use of shared transport */
808 EXPORT_SYMBOL_GPL(st_unregister);
810 /********************************************************************/
812 * functions called from TTY layer
814 static int st_tty_open(struct tty_struct *tty)
817 struct st_data_s *st_gdata;
818 pr_info("%s ", __func__);
820 st_kim_ref(&st_gdata, 0);
822 tty->disc_data = st_gdata;
824 /* don't do an wakeup for now */
825 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
827 /* mem already allocated
829 tty->receive_room = 65536;
830 /* Flush any pending characters in the driver and discipline. */
831 tty_ldisc_flush(tty);
832 tty_driver_flush_buffer(tty);
834 * signal to UIM via KIM that -
835 * installation of N_TI_WL ldisc is complete
837 st_kim_complete(st_gdata->kim_data);
838 pr_debug("done %s", __func__);
842 static void st_tty_close(struct tty_struct *tty)
844 unsigned char i = ST_MAX;
845 unsigned long flags = 0;
846 struct st_data_s *st_gdata = tty->disc_data;
848 pr_info("%s ", __func__);
851 * if a protocol has been registered & line discipline
852 * un-installed for some reason - what should be done ?
854 spin_lock_irqsave(&st_gdata->lock, flags);
855 for (i = ST_BT; i < ST_MAX; i++) {
856 if (st_gdata->list[i] != NULL)
857 pr_err("%d not un-registered", i);
858 st_gdata->list[i] = NULL;
860 st_gdata->protos_registered = 0;
861 spin_unlock_irqrestore(&st_gdata->lock, flags);
863 * signal to UIM via KIM that -
864 * N_TI_WL ldisc is un-installed
866 st_kim_complete(st_gdata->kim_data);
867 st_gdata->tty = NULL;
868 /* Flush any pending characters in the driver and discipline. */
869 tty_ldisc_flush(tty);
870 tty_driver_flush_buffer(tty);
872 spin_lock_irqsave(&st_gdata->lock, flags);
873 /* empty out txq and tx_waitq */
874 skb_queue_purge(&st_gdata->txq);
875 skb_queue_purge(&st_gdata->tx_waitq);
876 /* reset the TTY Rx states of ST */
877 st_gdata->rx_count = 0;
878 st_gdata->rx_state = ST_W4_PACKET_TYPE;
879 kfree_skb(st_gdata->rx_skb);
880 st_gdata->rx_skb = NULL;
881 spin_unlock_irqrestore(&st_gdata->lock, flags);
883 pr_debug("%s: done ", __func__);
886 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
887 char *tty_flags, int count)
891 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
892 16, 1, data, count, 0);
896 * if fw download is in progress then route incoming data
897 * to KIM for validation
899 st_recv(tty->disc_data, data, count);
900 pr_debug("done %s", __func__);
903 /* wake-up function called in from the TTY layer
904 * inside the internal wakeup function will be called
906 static void st_tty_wakeup(struct tty_struct *tty)
908 struct st_data_s *st_gdata = tty->disc_data;
909 pr_debug("%s ", __func__);
910 /* don't do an wakeup for now */
911 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
913 /* call our internal wakeup */
914 st_tx_wakeup((void *)st_gdata);
917 static void st_tty_flush_buffer(struct tty_struct *tty)
919 struct st_data_s *st_gdata = tty->disc_data;
920 pr_debug("%s ", __func__);
922 kfree_skb(st_gdata->tx_skb);
923 st_gdata->tx_skb = NULL;
925 tty->ops->flush_buffer(tty);
929 /********************************************************************/
930 int st_core_init(struct st_data_s **core_data)
932 struct st_data_s *st_gdata;
934 static struct tty_ldisc_ops *st_ldisc_ops;
936 /* populate and register to TTY line discipline */
937 st_ldisc_ops = kzalloc(sizeof(*st_ldisc_ops), GFP_KERNEL);
939 pr_err("no mem to allocate");
943 st_ldisc_ops->magic = TTY_LDISC_MAGIC;
944 st_ldisc_ops->name = "n_st"; /*"n_hci"; */
945 st_ldisc_ops->open = st_tty_open;
946 st_ldisc_ops->close = st_tty_close;
947 st_ldisc_ops->receive_buf = st_tty_receive;
948 st_ldisc_ops->write_wakeup = st_tty_wakeup;
949 st_ldisc_ops->flush_buffer = st_tty_flush_buffer;
950 st_ldisc_ops->owner = THIS_MODULE;
952 err = tty_register_ldisc(N_TI_WL, st_ldisc_ops);
954 pr_err("error registering %d line discipline %ld",
959 pr_debug("registered n_shared line discipline");
961 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
963 pr_err("memory allocation failed");
964 err = tty_unregister_ldisc(N_TI_WL);
966 pr_err("unable to un-register ldisc %ld", err);
972 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
973 * will be pushed in this queue for actual transmission.
975 skb_queue_head_init(&st_gdata->txq);
976 skb_queue_head_init(&st_gdata->tx_waitq);
978 /* Locking used in st_int_enqueue() to avoid multiple execution */
979 spin_lock_init(&st_gdata->lock);
981 /* ldisc_ops ref to be only used in __exit of module */
982 st_gdata->ldisc_ops = st_ldisc_ops;
987 pr_err("error during kim initialization(%ld)", err);
989 err = tty_unregister_ldisc(N_TI_WL);
991 pr_err("unable to un-register ldisc");
997 err = st_ll_init(st_gdata);
999 pr_err("error during st_ll initialization(%ld)", err);
1001 err = tty_unregister_ldisc(N_TI_WL);
1003 pr_err("unable to un-register ldisc");
1004 kfree(st_ldisc_ops);
1007 *core_data = st_gdata;
1011 void st_core_exit(struct st_data_s *st_gdata)
1014 /* internal module cleanup */
1015 err = st_ll_deinit(st_gdata);
1017 pr_err("error during deinit of ST LL %ld", err);
1019 err = st_kim_deinit();
1021 pr_err("error during deinit of ST KIM %ld", err);
1023 if (st_gdata != NULL) {
1024 /* Free ST Tx Qs and skbs */
1025 skb_queue_purge(&st_gdata->txq);
1026 skb_queue_purge(&st_gdata->tx_waitq);
1027 kfree_skb(st_gdata->rx_skb);
1028 kfree_skb(st_gdata->tx_skb);
1029 /* TTY ldisc cleanup */
1030 err = tty_unregister_ldisc(N_TI_WL);
1032 pr_err("unable to un-register ldisc %ld", err);
1033 kfree(st_gdata->ldisc_ops);
1034 /* free the global data pointer */