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>
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.
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.
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
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>
28 /* understand BT, FM and GPS for now */
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31 #include <net/bluetooth/hci.h>
32 #include <linux/ti_wilink_st.h>
34 /* strings to be used for rfkill entries and by
35 * ST Core to be used for sysfs debug entry
37 #define PROTO_ENTRY(type, name) name
38 const unsigned char *protocol_strngs[] = {
39 PROTO_ENTRY(ST_BT, "Bluetooth"),
40 PROTO_ENTRY(ST_FM, "FM"),
41 PROTO_ENTRY(ST_GPS, "GPS"),
43 /* function pointer pointing to either,
44 * st_kim_recv during registration to receive fw download responses
45 * st_int_recv after registration to receive proto stack responses
47 void (*st_recv) (void*, const unsigned char*, long);
49 /********************************************************************/
51 /* internal misc functions */
52 bool is_protocol_list_empty(void)
55 pr_debug(" %s ", __func__);
56 for (i = 0; i < ST_MAX; i++) {
57 if (st_gdata->list[i] != NULL)
66 /* can be called in from
67 * -- KIM (during fw download)
68 * -- ST Core (during st_write)
70 * This is the internal write function - a wrapper
73 int st_int_write(struct st_data_s *st_gdata,
74 const unsigned char *data, int count)
76 struct tty_struct *tty;
77 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
78 pr_err("tty unavailable to perform write");
83 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
84 16, 1, data, count, 0);
86 return tty->ops->write(tty, data, count);
91 * push the skb received to relevant
94 void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
96 pr_info(" %s(prot:%d) ", __func__, protoid);
99 (st_gdata == NULL || st_gdata->rx_skb == NULL
100 || st_gdata->list[protoid] == NULL)) {
101 pr_err("protocol %d not registered, no data to send?",
103 kfree_skb(st_gdata->rx_skb);
107 * this shouldn't take long
108 * - should be just skb_queue_tail for the
109 * protocol stack driver
111 if (likely(st_gdata->list[protoid]->recv != NULL)) {
113 (st_gdata->list[protoid]->recv
114 (st_gdata->list[protoid]->priv_data, st_gdata->rx_skb)
116 pr_err(" proto stack %d's ->recv failed", protoid);
117 kfree_skb(st_gdata->rx_skb);
121 pr_err(" proto stack %d's ->recv null", protoid);
122 kfree_skb(st_gdata->rx_skb);
129 * to call registration complete callbacks
130 * of all protocol stack drivers
132 void st_reg_complete(struct st_data_s *st_gdata, char err)
135 pr_info(" %s ", __func__);
136 for (i = 0; i < ST_MAX; i++) {
137 if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
138 st_gdata->list[i]->reg_complete_cb != NULL))
139 st_gdata->list[i]->reg_complete_cb
140 (st_gdata->list[i]->priv_data, err);
144 static inline int st_check_data_len(struct st_data_s *st_gdata,
145 int protoid, int len)
147 register int room = skb_tailroom(st_gdata->rx_skb);
149 pr_debug("len %d room %d", len, room);
152 /* Received packet has only packet header and
153 * has zero length payload. So, ask ST CORE to
154 * forward the packet to protocol driver (BT/FM/GPS)
156 st_send_frame(protoid, st_gdata);
158 } else if (len > room) {
159 /* Received packet's payload length is larger.
160 * We can't accommodate it in created skb.
162 pr_err("Data length is too large len %d room %d", len,
164 kfree_skb(st_gdata->rx_skb);
166 /* Packet header has non-zero payload length and
167 * we have enough space in created skb. Lets read
169 st_gdata->rx_state = ST_BT_W4_DATA;
170 st_gdata->rx_count = len;
174 /* Change ST state to continue to process next
176 st_gdata->rx_state = ST_W4_PACKET_TYPE;
177 st_gdata->rx_skb = NULL;
178 st_gdata->rx_count = 0;
184 * st_wakeup_ack - internal function for action when wake-up ack
187 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
190 register struct sk_buff *waiting_skb;
191 unsigned long flags = 0;
193 spin_lock_irqsave(&st_gdata->lock, flags);
194 /* de-Q from waitQ and Q in txQ now that the
197 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
198 skb_queue_tail(&st_gdata->txq, waiting_skb);
200 /* state forwarded to ST LL */
201 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
202 spin_unlock_irqrestore(&st_gdata->lock, flags);
204 /* wake up to send the recently copied skbs from waitQ */
205 st_tx_wakeup(st_gdata);
209 * st_int_recv - ST's internal receive function.
210 * Decodes received RAW data and forwards to corresponding
211 * client drivers (Bluetooth,FM,GPS..etc).
212 * This can receive various types of packets,
213 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
214 * CH-8 packets from FM, CH-9 packets from GPS cores.
216 void st_int_recv(void *disc_data,
217 const unsigned char *data, long count)
220 struct hci_event_hdr *eh;
221 struct hci_acl_hdr *ah;
222 struct hci_sco_hdr *sh;
223 struct fm_event_hdr *fm;
224 struct gps_event_hdr *gps;
225 register int len = 0, type = 0, dlen = 0;
226 static enum proto_type protoid = ST_MAX;
227 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
230 /* tty_receive sent null ? */
231 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
232 pr_err(" received null from TTY ");
236 pr_info("count %ld rx_state %ld"
237 "rx_count %ld", count, st_gdata->rx_state,
240 /* Decode received bytes here */
242 if (st_gdata->rx_count) {
243 len = min_t(unsigned int, st_gdata->rx_count, count);
244 memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
245 st_gdata->rx_count -= len;
249 if (st_gdata->rx_count)
252 /* Check ST RX state machine , where are we? */
253 switch (st_gdata->rx_state) {
255 /* Waiting for complete packet ? */
257 pr_debug("Complete pkt received");
259 /* Ask ST CORE to forward
260 * the packet to protocol driver */
261 st_send_frame(protoid, st_gdata);
263 st_gdata->rx_state = ST_W4_PACKET_TYPE;
264 st_gdata->rx_skb = NULL;
265 protoid = ST_MAX; /* is this required ? */
268 /* Waiting for Bluetooth event header ? */
269 case ST_BT_W4_EVENT_HDR:
270 eh = (struct hci_event_hdr *)st_gdata->rx_skb->
273 pr_debug("Event header: evt 0x%2.2x"
274 "plen %d", eh->evt, eh->plen);
276 st_check_data_len(st_gdata, protoid, eh->plen);
279 /* Waiting for Bluetooth acl header ? */
280 case ST_BT_W4_ACL_HDR:
281 ah = (struct hci_acl_hdr *)st_gdata->rx_skb->
283 dlen = __le16_to_cpu(ah->dlen);
285 pr_info("ACL header: dlen %d", dlen);
287 st_check_data_len(st_gdata, protoid, dlen);
290 /* Waiting for Bluetooth sco header ? */
291 case ST_BT_W4_SCO_HDR:
292 sh = (struct hci_sco_hdr *)st_gdata->rx_skb->
295 pr_info("SCO header: dlen %d", sh->dlen);
297 st_check_data_len(st_gdata, protoid, sh->dlen);
299 case ST_FM_W4_EVENT_HDR:
300 fm = (struct fm_event_hdr *)st_gdata->rx_skb->
302 pr_info("FM Header: ");
303 st_check_data_len(st_gdata, ST_FM, fm->plen);
305 /* TODO : Add GPS packet machine logic here */
306 case ST_GPS_W4_EVENT_HDR:
307 /* [0x09 pkt hdr][R/W byte][2 byte len] */
308 gps = (struct gps_event_hdr *)st_gdata->rx_skb->
310 pr_info("GPS Header: ");
311 st_check_data_len(st_gdata, ST_GPS, gps->plen);
313 } /* end of switch rx_state */
316 /* end of if rx_count */
317 /* Check first byte of packet and identify module
318 * owner (BT/FM/GPS) */
321 /* Bluetooth event packet? */
323 pr_info("Event packet");
324 st_gdata->rx_state = ST_BT_W4_EVENT_HDR;
325 st_gdata->rx_count = HCI_EVENT_HDR_SIZE;
326 type = HCI_EVENT_PKT;
330 /* Bluetooth acl packet? */
331 case HCI_ACLDATA_PKT:
332 pr_info("ACL packet");
333 st_gdata->rx_state = ST_BT_W4_ACL_HDR;
334 st_gdata->rx_count = HCI_ACL_HDR_SIZE;
335 type = HCI_ACLDATA_PKT;
339 /* Bluetooth sco packet? */
340 case HCI_SCODATA_PKT:
341 pr_info("SCO packet");
342 st_gdata->rx_state = ST_BT_W4_SCO_HDR;
343 st_gdata->rx_count = HCI_SCO_HDR_SIZE;
344 type = HCI_SCODATA_PKT;
348 /* Channel 8(FM) packet? */
350 pr_info("FM CH8 packet");
351 type = ST_FM_CH8_PKT;
352 st_gdata->rx_state = ST_FM_W4_EVENT_HDR;
353 st_gdata->rx_count = FM_EVENT_HDR_SIZE;
357 /* Channel 9(GPS) packet? */
358 case 0x9: /*ST_LL_GPS_CH9_PKT */
359 pr_info("GPS CH9 packet");
360 type = 0x9; /* ST_LL_GPS_CH9_PKT; */
362 st_gdata->rx_state = ST_GPS_W4_EVENT_HDR;
363 st_gdata->rx_count = 3; /* GPS_EVENT_HDR_SIZE -1*/
368 pr_info("PM packet");
369 /* this takes appropriate action based on
370 * sleep state received --
372 st_ll_sleep_state(st_gdata, *ptr);
377 pr_info("PM packet");
378 /* wake up ack received */
379 st_wakeup_ack(st_gdata, *ptr);
385 pr_err("Unknown packet type %2.2x", (__u8) *ptr);
395 /* Allocate new packet to hold received data */
397 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
398 if (!st_gdata->rx_skb) {
399 pr_err("Can't allocate mem for new packet");
400 st_gdata->rx_state = ST_W4_PACKET_TYPE;
401 st_gdata->rx_count = 0;
404 bt_cb(st_gdata->rx_skb)->pkt_type = type;
406 case ST_FM: /* for FM */
408 alloc_skb(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
409 if (!st_gdata->rx_skb) {
410 pr_err("Can't allocate mem for new packet");
411 st_gdata->rx_state = ST_W4_PACKET_TYPE;
412 st_gdata->rx_count = 0;
415 /* place holder 0x08 */
416 skb_reserve(st_gdata->rx_skb, 1);
417 st_gdata->rx_skb->cb[0] = ST_FM_CH8_PKT;
422 alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC);
423 if (!st_gdata->rx_skb) {
424 pr_err("Can't allocate mem for new packet");
425 st_gdata->rx_state = ST_W4_PACKET_TYPE;
426 st_gdata->rx_count = 0;
429 /* place holder 0x09 */
430 skb_reserve(st_gdata->rx_skb, 1);
431 st_gdata->rx_skb->cb[0] = 0x09; /*ST_GPS_CH9_PKT; */
437 pr_debug("done %s", __func__);
442 * st_int_dequeue - internal de-Q function.
443 * If the previous data set was not written
444 * completely, return that skb which has the pending data.
445 * In normal cases, return top of txq.
447 struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
449 struct sk_buff *returning_skb;
451 pr_debug("%s", __func__);
452 if (st_gdata->tx_skb != NULL) {
453 returning_skb = st_gdata->tx_skb;
454 st_gdata->tx_skb = NULL;
455 return returning_skb;
457 return skb_dequeue(&st_gdata->txq);
461 * st_int_enqueue - internal Q-ing function.
462 * Will either Q the skb to txq or the tx_waitq
463 * depending on the ST LL state.
464 * If the chip is asleep, then Q it onto waitq and
466 * txq and waitq needs protection since the other contexts
467 * may be sending data, waking up chip.
469 void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
471 unsigned long flags = 0;
473 pr_debug("%s", __func__);
474 spin_lock_irqsave(&st_gdata->lock, flags);
476 switch (st_ll_getstate(st_gdata)) {
478 pr_info("ST LL is AWAKE, sending normally");
479 skb_queue_tail(&st_gdata->txq, skb);
481 case ST_LL_ASLEEP_TO_AWAKE:
482 skb_queue_tail(&st_gdata->tx_waitq, skb);
484 case ST_LL_AWAKE_TO_ASLEEP:
485 pr_err("ST LL is illegal state(%ld),"
486 "purging received skb.", st_ll_getstate(st_gdata));
490 skb_queue_tail(&st_gdata->tx_waitq, skb);
491 st_ll_wakeup(st_gdata);
494 pr_err("ST LL is illegal state(%ld),"
495 "purging received skb.", st_ll_getstate(st_gdata));
500 spin_unlock_irqrestore(&st_gdata->lock, flags);
501 pr_debug("done %s", __func__);
506 * internal wakeup function
508 * - TTY layer when write's finished
509 * - st_write (in context of the protocol stack)
511 void st_tx_wakeup(struct st_data_s *st_data)
514 unsigned long flags; /* for irq save flags */
515 pr_debug("%s", __func__);
516 /* check for sending & set flag sending here */
517 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
518 pr_info("ST already sending");
520 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
522 /* TX_WAKEUP will be checked in another
526 do { /* come back if st_tx_wakeup is set */
527 /* woke-up to write */
528 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
529 while ((skb = st_int_dequeue(st_data))) {
531 spin_lock_irqsave(&st_data->lock, flags);
532 /* enable wake-up from TTY */
533 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
534 len = st_int_write(st_data, skb->data, skb->len);
536 /* if skb->len = len as expected, skb->len=0 */
538 /* would be the next skb to be sent */
539 st_data->tx_skb = skb;
540 spin_unlock_irqrestore(&st_data->lock, flags);
544 spin_unlock_irqrestore(&st_data->lock, flags);
546 /* if wake-up is set in another context- restart sending */
547 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
549 /* clear flag sending */
550 clear_bit(ST_TX_SENDING, &st_data->tx_state);
553 /********************************************************************/
554 /* functions called from ST KIM
556 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
558 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
559 st_gdata->protos_registered,
560 st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
561 st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
562 st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
565 /********************************************************************/
567 * functions called from protocol stack drivers
570 long st_register(struct st_proto_s *new_proto)
572 struct st_data_s *st_gdata;
574 unsigned long flags = 0;
576 st_kim_ref(&st_gdata, 0);
577 pr_info("%s(%d) ", __func__, new_proto->type);
578 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
579 || new_proto->reg_complete_cb == NULL) {
580 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
584 if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
585 pr_err("protocol %d not supported", new_proto->type);
586 return -EPROTONOSUPPORT;
589 if (st_gdata->list[new_proto->type] != NULL) {
590 pr_err("protocol %d already registered", new_proto->type);
594 /* can be from process context only */
595 spin_lock_irqsave(&st_gdata->lock, flags);
597 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
598 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
599 /* fw download in progress */
600 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
602 st_gdata->list[new_proto->type] = new_proto;
603 st_gdata->protos_registered++;
604 new_proto->write = st_write;
606 set_bit(ST_REG_PENDING, &st_gdata->st_state);
607 spin_unlock_irqrestore(&st_gdata->lock, flags);
609 } else if (st_gdata->protos_registered == ST_EMPTY) {
610 pr_info(" protocol list empty :%d ", new_proto->type);
611 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
612 st_recv = st_kim_recv;
614 /* release lock previously held - re-locked below */
615 spin_unlock_irqrestore(&st_gdata->lock, flags);
617 /* enable the ST LL - to set default chip state */
618 st_ll_enable(st_gdata);
619 /* this may take a while to complete
620 * since it involves BT fw download
622 err = st_kim_start(st_gdata->kim_data);
624 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
625 if ((st_gdata->protos_registered != ST_EMPTY) &&
626 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
627 pr_err(" KIM failure complete callback ");
628 st_reg_complete(st_gdata, -1);
634 /* the protocol might require other gpios to be toggled
636 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
638 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
639 st_recv = st_int_recv;
641 /* this is where all pending registration
642 * are signalled to be complete by calling callback functions
644 if ((st_gdata->protos_registered != ST_EMPTY) &&
645 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
646 pr_debug(" call reg complete callback ");
647 st_reg_complete(st_gdata, 0);
649 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
651 /* check for already registered once more,
652 * since the above check is old
654 if (st_gdata->list[new_proto->type] != NULL) {
655 pr_err(" proto %d already registered ",
660 spin_lock_irqsave(&st_gdata->lock, flags);
661 st_gdata->list[new_proto->type] = new_proto;
662 st_gdata->protos_registered++;
663 new_proto->write = st_write;
664 spin_unlock_irqrestore(&st_gdata->lock, flags);
667 /* if fw is already downloaded & new stack registers protocol */
669 switch (new_proto->type) {
675 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
679 pr_err("%d protocol not supported",
681 spin_unlock_irqrestore(&st_gdata->lock, flags);
682 return -EPROTONOSUPPORT;
684 st_gdata->list[new_proto->type] = new_proto;
685 st_gdata->protos_registered++;
686 new_proto->write = st_write;
688 /* lock already held before entering else */
689 spin_unlock_irqrestore(&st_gdata->lock, flags);
692 pr_debug("done %s(%d) ", __func__, new_proto->type);
694 EXPORT_SYMBOL_GPL(st_register);
696 /* to unregister a protocol -
697 * to be called from protocol stack driver
699 long st_unregister(enum proto_type type)
702 unsigned long flags = 0;
703 struct st_data_s *st_gdata;
705 pr_debug("%s: %d ", __func__, type);
707 st_kim_ref(&st_gdata, 0);
708 if (type < ST_BT || type >= ST_MAX) {
709 pr_err(" protocol %d not supported", type);
710 return -EPROTONOSUPPORT;
713 spin_lock_irqsave(&st_gdata->lock, flags);
715 if (st_gdata->list[type] == NULL) {
716 pr_err(" protocol %d not registered", type);
717 spin_unlock_irqrestore(&st_gdata->lock, flags);
718 return -EPROTONOSUPPORT;
721 st_gdata->protos_registered--;
722 st_gdata->list[type] = NULL;
724 /* kim ignores BT in the below function
725 * and handles the rest, BT is toggled
726 * only in kim_start and kim_stop
728 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
729 spin_unlock_irqrestore(&st_gdata->lock, flags);
731 if ((st_gdata->protos_registered == ST_EMPTY) &&
732 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
733 pr_info(" all protocols unregistered ");
735 /* stop traffic on tty */
737 tty_ldisc_flush(st_gdata->tty);
738 stop_tty(st_gdata->tty);
741 /* all protocols now unregistered */
742 st_kim_stop(st_gdata->kim_data);
744 st_ll_disable(st_gdata);
750 * called in protocol stack drivers
751 * via the write function pointer
753 long st_write(struct sk_buff *skb)
755 struct st_data_s *st_gdata;
757 enum proto_type protoid = ST_MAX;
761 st_kim_ref(&st_gdata, 0);
762 if (unlikely(skb == NULL || st_gdata == NULL
763 || st_gdata->tty == NULL)) {
764 pr_err("data/tty unavailable to perform write");
767 #ifdef DEBUG /* open-up skb to read the 1st byte */
768 switch (skb->data[0]) {
769 case HCI_COMMAND_PKT:
770 case HCI_ACLDATA_PKT:
771 case HCI_SCODATA_PKT:
781 if (unlikely(st_gdata->list[protoid] == NULL)) {
782 pr_err(" protocol %d not registered, and writing? ",
787 pr_debug("%d to be written", skb->len);
790 /* st_ll to decide where to enqueue the skb */
791 st_int_enqueue(st_gdata, skb);
793 st_tx_wakeup(st_gdata);
795 /* return number of bytes written */
799 /* for protocols making use of shared transport */
800 EXPORT_SYMBOL_GPL(st_unregister);
802 /********************************************************************/
804 * functions called from TTY layer
806 static int st_tty_open(struct tty_struct *tty)
809 struct st_data_s *st_gdata;
810 pr_info("%s ", __func__);
812 st_kim_ref(&st_gdata, 0);
814 tty->disc_data = st_gdata;
816 /* don't do an wakeup for now */
817 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
819 /* mem already allocated
821 tty->receive_room = 65536;
822 /* Flush any pending characters in the driver and discipline. */
823 tty_ldisc_flush(tty);
824 tty_driver_flush_buffer(tty);
826 * signal to UIM via KIM that -
827 * installation of N_TI_WL ldisc is complete
829 st_kim_complete(st_gdata->kim_data);
830 pr_debug("done %s", __func__);
834 static void st_tty_close(struct tty_struct *tty)
836 unsigned char i = ST_MAX;
837 unsigned long flags = 0;
838 struct st_data_s *st_gdata = tty->disc_data;
840 pr_info("%s ", __func__);
843 * if a protocol has been registered & line discipline
844 * un-installed for some reason - what should be done ?
846 spin_lock_irqsave(&st_gdata->lock, flags);
847 for (i = ST_BT; i < ST_MAX; i++) {
848 if (st_gdata->list[i] != NULL)
849 pr_err("%d not un-registered", i);
850 st_gdata->list[i] = NULL;
852 st_gdata->protos_registered = 0;
853 spin_unlock_irqrestore(&st_gdata->lock, flags);
855 * signal to UIM via KIM that -
856 * N_TI_WL ldisc is un-installed
858 st_kim_complete(st_gdata->kim_data);
859 st_gdata->tty = NULL;
860 /* Flush any pending characters in the driver and discipline. */
861 tty_ldisc_flush(tty);
862 tty_driver_flush_buffer(tty);
864 spin_lock_irqsave(&st_gdata->lock, flags);
865 /* empty out txq and tx_waitq */
866 skb_queue_purge(&st_gdata->txq);
867 skb_queue_purge(&st_gdata->tx_waitq);
868 /* reset the TTY Rx states of ST */
869 st_gdata->rx_count = 0;
870 st_gdata->rx_state = ST_W4_PACKET_TYPE;
871 kfree_skb(st_gdata->rx_skb);
872 st_gdata->rx_skb = NULL;
873 spin_unlock_irqrestore(&st_gdata->lock, flags);
875 pr_debug("%s: done ", __func__);
878 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
879 char *tty_flags, int count)
883 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
884 16, 1, data, count, 0);
888 * if fw download is in progress then route incoming data
889 * to KIM for validation
891 st_recv(tty->disc_data, data, count);
892 pr_debug("done %s", __func__);
895 /* wake-up function called in from the TTY layer
896 * inside the internal wakeup function will be called
898 static void st_tty_wakeup(struct tty_struct *tty)
900 struct st_data_s *st_gdata = tty->disc_data;
901 pr_debug("%s ", __func__);
902 /* don't do an wakeup for now */
903 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
905 /* call our internal wakeup */
906 st_tx_wakeup((void *)st_gdata);
909 static void st_tty_flush_buffer(struct tty_struct *tty)
911 struct st_data_s *st_gdata = tty->disc_data;
912 pr_debug("%s ", __func__);
914 kfree_skb(st_gdata->tx_skb);
915 st_gdata->tx_skb = NULL;
917 tty->ops->flush_buffer(tty);
921 /********************************************************************/
922 int st_core_init(struct st_data_s **core_data)
924 struct st_data_s *st_gdata;
926 static struct tty_ldisc_ops *st_ldisc_ops;
928 /* populate and register to TTY line discipline */
929 st_ldisc_ops = kzalloc(sizeof(*st_ldisc_ops), GFP_KERNEL);
931 pr_err("no mem to allocate");
935 st_ldisc_ops->magic = TTY_LDISC_MAGIC;
936 st_ldisc_ops->name = "n_st"; /*"n_hci"; */
937 st_ldisc_ops->open = st_tty_open;
938 st_ldisc_ops->close = st_tty_close;
939 st_ldisc_ops->receive_buf = st_tty_receive;
940 st_ldisc_ops->write_wakeup = st_tty_wakeup;
941 st_ldisc_ops->flush_buffer = st_tty_flush_buffer;
942 st_ldisc_ops->owner = THIS_MODULE;
944 err = tty_register_ldisc(N_TI_WL, st_ldisc_ops);
946 pr_err("error registering %d line discipline %ld",
951 pr_debug("registered n_shared line discipline");
953 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
955 pr_err("memory allocation failed");
956 err = tty_unregister_ldisc(N_TI_WL);
958 pr_err("unable to un-register ldisc %ld", err);
964 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
965 * will be pushed in this queue for actual transmission.
967 skb_queue_head_init(&st_gdata->txq);
968 skb_queue_head_init(&st_gdata->tx_waitq);
970 /* Locking used in st_int_enqueue() to avoid multiple execution */
971 spin_lock_init(&st_gdata->lock);
973 /* ldisc_ops ref to be only used in __exit of module */
974 st_gdata->ldisc_ops = st_ldisc_ops;
979 pr_err("error during kim initialization(%ld)", err);
981 err = tty_unregister_ldisc(N_TI_WL);
983 pr_err("unable to un-register ldisc");
989 err = st_ll_init(st_gdata);
991 pr_err("error during st_ll initialization(%ld)", err);
993 err = tty_unregister_ldisc(N_TI_WL);
995 pr_err("unable to un-register ldisc");
999 *core_data = st_gdata;
1003 void st_core_exit(struct st_data_s *st_gdata)
1006 /* internal module cleanup */
1007 err = st_ll_deinit(st_gdata);
1009 pr_err("error during deinit of ST LL %ld", err);
1011 err = st_kim_deinit();
1013 pr_err("error during deinit of ST KIM %ld", err);
1015 if (st_gdata != NULL) {
1016 /* Free ST Tx Qs and skbs */
1017 skb_queue_purge(&st_gdata->txq);
1018 skb_queue_purge(&st_gdata->tx_waitq);
1019 kfree_skb(st_gdata->rx_skb);
1020 kfree_skb(st_gdata->tx_skb);
1021 /* TTY ldisc cleanup */
1022 err = tty_unregister_ldisc(N_TI_WL);
1024 pr_err("unable to un-register ldisc %ld", err);
1025 kfree(st_gdata->ldisc_ops);
1026 /* free the global data pointer */