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
43 /* strings to be used for rfkill entries and by
44 * ST Core to be used for sysfs debug entry
46 #define PROTO_ENTRY(type, name) name
47 const unsigned char *protocol_strngs[] = {
48 PROTO_ENTRY(ST_BT, "Bluetooth"),
49 PROTO_ENTRY(ST_FM, "FM"),
50 PROTO_ENTRY(ST_GPS, "GPS"),
53 /* function pointer pointing to either,
54 * st_kim_recv during registration to receive fw download responses
55 * st_int_recv after registration to receive proto stack responses
57 void (*st_recv) (void*, const unsigned char*, long);
59 /********************************************************************/
61 /* internal misc functions */
62 bool is_protocol_list_empty(void)
65 pr_debug(" %s ", __func__);
66 for (i = 0; i < ST_MAX; i++) {
67 if (st_gdata->list[i] != NULL)
75 /* can be called in from
76 * -- KIM (during fw download)
77 * -- ST Core (during st_write)
79 * This is the internal write function - a wrapper
82 int st_int_write(struct st_data_s *st_gdata,
83 const unsigned char *data, int count)
85 struct tty_struct *tty;
86 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
87 pr_err("tty unavailable to perform write");
92 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
93 16, 1, data, count, 0);
95 return tty->ops->write(tty, data, count);
100 * push the skb received to relevant
103 void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
105 pr_info(" %s(prot:%d) ", __func__, protoid);
108 (st_gdata == NULL || st_gdata->rx_skb == NULL
109 || st_gdata->list[protoid] == NULL)) {
110 pr_err("protocol %d not registered, no data to send?",
112 kfree_skb(st_gdata->rx_skb);
116 * this shouldn't take long
117 * - should be just skb_queue_tail for the
118 * protocol stack driver
120 if (likely(st_gdata->list[protoid]->recv != NULL)) {
121 if (unlikely(st_gdata->list[protoid]->recv(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);
135 * to call registration complete callbacks
136 * of all protocol stack drivers
138 void st_reg_complete(struct st_data_s *st_gdata, char err)
141 pr_info(" %s ", __func__);
142 for (i = 0; i < ST_MAX; i++) {
143 if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
144 st_gdata->list[i]->reg_complete_cb != NULL))
145 st_gdata->list[i]->reg_complete_cb(err);
149 static inline int st_check_data_len(struct st_data_s *st_gdata,
150 int protoid, int len)
152 register int room = skb_tailroom(st_gdata->rx_skb);
154 pr_debug("len %d room %d", len, room);
157 /* Received packet has only packet header and
158 * has zero length payload. So, ask ST CORE to
159 * forward the packet to protocol driver (BT/FM/GPS)
161 st_send_frame(protoid, st_gdata);
163 } else if (len > room) {
164 /* Received packet's payload length is larger.
165 * We can't accommodate it in created skb.
167 pr_err("Data length is too large len %d room %d", len,
169 kfree_skb(st_gdata->rx_skb);
171 /* Packet header has non-zero payload length and
172 * we have enough space in created skb. Lets read
174 st_gdata->rx_state = ST_BT_W4_DATA;
175 st_gdata->rx_count = len;
179 /* Change ST state to continue to process next
181 st_gdata->rx_state = ST_W4_PACKET_TYPE;
182 st_gdata->rx_skb = NULL;
183 st_gdata->rx_count = 0;
188 /* internal function for action when wake-up ack
191 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
194 register struct sk_buff *waiting_skb;
195 unsigned long flags = 0;
197 spin_lock_irqsave(&st_gdata->lock, flags);
198 /* de-Q from waitQ and Q in txQ now that the
201 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
202 skb_queue_tail(&st_gdata->txq, waiting_skb);
204 /* state forwarded to ST LL */
205 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
206 spin_unlock_irqrestore(&st_gdata->lock, flags);
208 /* wake up to send the recently copied skbs from waitQ */
209 st_tx_wakeup(st_gdata);
212 /* Decodes received RAW data and forwards to corresponding
213 * client drivers (Bluetooth,FM,GPS..etc).
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__);
441 /* internal de-Q function
442 * -- return previous in-completely written skb
443 * or return the skb in the txQ
445 struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
447 struct sk_buff *returning_skb;
449 pr_debug("%s", __func__);
450 /* if the previous skb wasn't written completely
452 if (st_gdata->tx_skb != NULL) {
453 returning_skb = st_gdata->tx_skb;
454 st_gdata->tx_skb = NULL;
455 return returning_skb;
458 /* de-Q from the txQ always if previous write is complete */
459 return skb_dequeue(&st_gdata->txq);
462 /* internal Q-ing function
463 * will either Q the skb to txq or the tx_waitq
464 * depending on the ST LL state
466 * lock the whole func - since ll_getstate and Q-ing should happen
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 /* this function can be invoked in more then one context.
476 spin_lock_irqsave(&st_gdata->lock, flags);
478 switch (st_ll_getstate(st_gdata)) {
480 pr_info("ST LL is AWAKE, sending normally");
481 skb_queue_tail(&st_gdata->txq, skb);
483 case ST_LL_ASLEEP_TO_AWAKE:
484 skb_queue_tail(&st_gdata->tx_waitq, skb);
486 case ST_LL_AWAKE_TO_ASLEEP: /* host cannot be in this state */
487 pr_err("ST LL is illegal state(%ld),"
488 "purging received skb.", st_ll_getstate(st_gdata));
493 /* call a function of ST LL to put data
494 * in tx_waitQ and wake_ind in txQ
496 skb_queue_tail(&st_gdata->tx_waitq, skb);
497 st_ll_wakeup(st_gdata);
500 pr_err("ST LL is illegal state(%ld),"
501 "purging received skb.", st_ll_getstate(st_gdata));
505 spin_unlock_irqrestore(&st_gdata->lock, flags);
506 pr_debug("done %s", __func__);
511 * internal wakeup function
513 * - TTY layer when write's finished
514 * - st_write (in context of the protocol stack)
516 void st_tx_wakeup(struct st_data_s *st_data)
519 unsigned long flags; /* for irq save flags */
520 pr_debug("%s", __func__);
521 /* check for sending & set flag sending here */
522 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
523 pr_info("ST already sending");
525 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
527 /* TX_WAKEUP will be checked in another
531 do { /* come back if st_tx_wakeup is set */
532 /* woke-up to write */
533 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
534 while ((skb = st_int_dequeue(st_data))) {
536 spin_lock_irqsave(&st_data->lock, flags);
537 /* enable wake-up from TTY */
538 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
539 len = st_int_write(st_data, skb->data, skb->len);
541 /* if skb->len = len as expected, skb->len=0 */
543 /* would be the next skb to be sent */
544 st_data->tx_skb = skb;
545 spin_unlock_irqrestore(&st_data->lock, flags);
549 spin_unlock_irqrestore(&st_data->lock, flags);
551 /* if wake-up is set in another context- restart sending */
552 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
554 /* clear flag sending */
555 clear_bit(ST_TX_SENDING, &st_data->tx_state);
558 /********************************************************************/
559 /* functions called from ST KIM
561 void kim_st_list_protocols(struct st_data_s *st_gdata, char *buf)
563 unsigned long flags = 0;
565 unsigned char i = ST_MAX;
567 spin_lock_irqsave(&st_gdata->lock, flags);
568 #ifdef DEBUG /* more detailed log */
569 for (i = 0; i < ST_MAX; i++) {
571 sprintf(buf, "%s is %s", protocol_strngs[i],
573 NULL ? "Registered" : "Unregistered");
575 sprintf(buf, "%s\n%s is %s", buf, protocol_strngs[i],
577 NULL ? "Registered" : "Unregistered");
580 sprintf(buf, "%s\n", buf);
581 #else /* limited info */
582 sprintf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
583 st_gdata->protos_registered,
584 st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
585 st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
586 st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
588 spin_unlock_irqrestore(&st_gdata->lock, flags);
591 /********************************************************************/
593 * functions called from protocol stack drivers
596 long st_register(struct st_proto_s *new_proto)
598 struct st_data_s *st_gdata;
600 unsigned long flags = 0;
602 st_kim_ref(&st_gdata);
603 pr_info("%s(%d) ", __func__, new_proto->type);
604 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
605 || new_proto->reg_complete_cb == NULL) {
606 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
610 if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
611 pr_err("protocol %d not supported", new_proto->type);
612 return -EPROTONOSUPPORT;
615 if (st_gdata->list[new_proto->type] != NULL) {
616 pr_err("protocol %d already registered", new_proto->type);
620 /* can be from process context only */
621 spin_lock_irqsave(&st_gdata->lock, flags);
623 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
624 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
625 /* fw download in progress */
626 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
628 st_gdata->list[new_proto->type] = new_proto;
629 st_gdata->protos_registered++;
630 new_proto->write = st_write;
632 set_bit(ST_REG_PENDING, &st_gdata->st_state);
633 spin_unlock_irqrestore(&st_gdata->lock, flags);
635 } else if (st_gdata->protos_registered == ST_EMPTY) {
636 pr_info(" protocol list empty :%d ", new_proto->type);
637 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
638 st_recv = st_kim_recv;
640 /* release lock previously held - re-locked below */
641 spin_unlock_irqrestore(&st_gdata->lock, flags);
643 /* enable the ST LL - to set default chip state */
644 st_ll_enable(st_gdata);
645 /* this may take a while to complete
646 * since it involves BT fw download
648 err = st_kim_start(st_gdata->kim_data);
650 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
651 if ((st_gdata->protos_registered != ST_EMPTY) &&
652 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
653 pr_err(" KIM failure complete callback ");
654 st_reg_complete(st_gdata, -1);
660 /* the protocol might require other gpios to be toggled
662 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
664 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
665 st_recv = st_int_recv;
667 /* this is where all pending registration
668 * are signalled to be complete by calling callback functions
670 if ((st_gdata->protos_registered != ST_EMPTY) &&
671 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
672 pr_debug(" call reg complete callback ");
673 st_reg_complete(st_gdata, 0);
675 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
677 /* check for already registered once more,
678 * since the above check is old
680 if (st_gdata->list[new_proto->type] != NULL) {
681 pr_err(" proto %d already registered ",
686 spin_lock_irqsave(&st_gdata->lock, flags);
687 st_gdata->list[new_proto->type] = new_proto;
688 st_gdata->protos_registered++;
689 new_proto->write = st_write;
690 spin_unlock_irqrestore(&st_gdata->lock, flags);
693 /* if fw is already downloaded & new stack registers protocol */
695 switch (new_proto->type) {
701 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
705 pr_err("%d protocol not supported",
707 err = -EPROTONOSUPPORT;
708 /* something wrong */
711 st_gdata->list[new_proto->type] = new_proto;
712 st_gdata->protos_registered++;
713 new_proto->write = st_write;
715 /* lock already held before entering else */
716 spin_unlock_irqrestore(&st_gdata->lock, flags);
719 pr_debug("done %s(%d) ", __func__, new_proto->type);
721 EXPORT_SYMBOL_GPL(st_register);
723 /* to unregister a protocol -
724 * to be called from protocol stack driver
726 long st_unregister(enum proto_type type)
729 unsigned long flags = 0;
730 struct st_data_s *st_gdata;
732 pr_debug("%s: %d ", __func__, type);
734 st_kim_ref(&st_gdata);
735 if (type < ST_BT || type >= ST_MAX) {
736 pr_err(" protocol %d not supported", type);
737 return -EPROTONOSUPPORT;
740 spin_lock_irqsave(&st_gdata->lock, flags);
742 if (st_gdata->list[type] == NULL) {
743 pr_err(" protocol %d not registered", type);
744 spin_unlock_irqrestore(&st_gdata->lock, flags);
745 return -EPROTONOSUPPORT;
748 st_gdata->protos_registered--;
749 st_gdata->list[type] = NULL;
751 /* kim ignores BT in the below function
752 * and handles the rest, BT is toggled
753 * only in kim_start and kim_stop
755 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
756 spin_unlock_irqrestore(&st_gdata->lock, flags);
758 if ((st_gdata->protos_registered == ST_EMPTY) &&
759 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
760 pr_info(" all protocols unregistered ");
762 /* stop traffic on tty */
764 tty_ldisc_flush(st_gdata->tty);
765 stop_tty(st_gdata->tty);
768 /* all protocols now unregistered */
769 st_kim_stop(st_gdata->kim_data);
771 st_ll_disable(st_gdata);
777 * called in protocol stack drivers
778 * via the write function pointer
780 long st_write(struct sk_buff *skb)
782 struct st_data_s *st_gdata;
784 enum proto_type protoid = ST_MAX;
788 st_kim_ref(&st_gdata);
789 if (unlikely(skb == NULL || st_gdata == NULL
790 || st_gdata->tty == NULL)) {
791 pr_err("data/tty unavailable to perform write");
794 #ifdef DEBUG /* open-up skb to read the 1st byte */
795 switch (skb->data[0]) {
796 case HCI_COMMAND_PKT:
797 case HCI_ACLDATA_PKT:
798 case HCI_SCODATA_PKT:
808 if (unlikely(st_gdata->list[protoid] == NULL)) {
809 pr_err(" protocol %d not registered, and writing? ",
814 pr_debug("%d to be written", skb->len);
817 /* st_ll to decide where to enqueue the skb */
818 st_int_enqueue(st_gdata, skb);
820 st_tx_wakeup(st_gdata);
822 /* return number of bytes written */
826 /* for protocols making use of shared transport */
827 EXPORT_SYMBOL_GPL(st_unregister);
829 /********************************************************************/
831 * functions called from TTY layer
833 static int st_tty_open(struct tty_struct *tty)
836 struct st_data_s *st_gdata;
837 pr_info("%s ", __func__);
839 st_kim_ref(&st_gdata);
841 tty->disc_data = st_gdata;
843 /* don't do an wakeup for now */
844 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
846 /* mem already allocated
848 tty->receive_room = 65536;
849 /* Flush any pending characters in the driver and discipline. */
850 tty_ldisc_flush(tty);
851 tty_driver_flush_buffer(tty);
853 * signal to UIM via KIM that -
854 * installation of N_TI_WL ldisc is complete
856 st_kim_complete(st_gdata->kim_data);
857 pr_debug("done %s", __func__);
861 static void st_tty_close(struct tty_struct *tty)
863 unsigned char i = ST_MAX;
864 unsigned long flags = 0;
865 struct st_data_s *st_gdata = tty->disc_data;
867 pr_info("%s ", __func__);
870 * if a protocol has been registered & line discipline
871 * un-installed for some reason - what should be done ?
873 spin_lock_irqsave(&st_gdata->lock, flags);
874 for (i = ST_BT; i < ST_MAX; i++) {
875 if (st_gdata->list[i] != NULL)
876 pr_err("%d not un-registered", i);
877 st_gdata->list[i] = NULL;
879 spin_unlock_irqrestore(&st_gdata->lock, flags);
881 * signal to UIM via KIM that -
882 * N_TI_WL ldisc is un-installed
884 st_kim_complete(st_gdata->kim_data);
885 st_gdata->tty = NULL;
886 /* Flush any pending characters in the driver and discipline. */
887 tty_ldisc_flush(tty);
888 tty_driver_flush_buffer(tty);
890 spin_lock_irqsave(&st_gdata->lock, flags);
891 /* empty out txq and tx_waitq */
892 skb_queue_purge(&st_gdata->txq);
893 skb_queue_purge(&st_gdata->tx_waitq);
894 /* reset the TTY Rx states of ST */
895 st_gdata->rx_count = 0;
896 st_gdata->rx_state = ST_W4_PACKET_TYPE;
897 kfree_skb(st_gdata->rx_skb);
898 st_gdata->rx_skb = NULL;
899 spin_unlock_irqrestore(&st_gdata->lock, flags);
901 pr_debug("%s: done ", __func__);
904 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
905 char *tty_flags, int count)
909 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
910 16, 1, data, count, 0);
914 * if fw download is in progress then route incoming data
915 * to KIM for validation
917 st_recv(tty->disc_data, data, count);
918 pr_debug("done %s", __func__);
921 /* wake-up function called in from the TTY layer
922 * inside the internal wakeup function will be called
924 static void st_tty_wakeup(struct tty_struct *tty)
926 struct st_data_s *st_gdata = tty->disc_data;
927 pr_debug("%s ", __func__);
928 /* don't do an wakeup for now */
929 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
931 /* call our internal wakeup */
932 st_tx_wakeup((void *)st_gdata);
935 static void st_tty_flush_buffer(struct tty_struct *tty)
937 struct st_data_s *st_gdata = tty->disc_data;
938 pr_debug("%s ", __func__);
940 kfree_skb(st_gdata->tx_skb);
941 st_gdata->tx_skb = NULL;
943 tty->ops->flush_buffer(tty);
947 /********************************************************************/
948 int st_core_init(struct st_data_s **core_data)
950 struct st_data_s *st_gdata;
952 static struct tty_ldisc_ops *st_ldisc_ops;
954 /* populate and register to TTY line discipline */
955 st_ldisc_ops = kzalloc(sizeof(*st_ldisc_ops), GFP_KERNEL);
957 pr_err("no mem to allocate");
961 st_ldisc_ops->magic = TTY_LDISC_MAGIC;
962 st_ldisc_ops->name = "n_st"; /*"n_hci"; */
963 st_ldisc_ops->open = st_tty_open;
964 st_ldisc_ops->close = st_tty_close;
965 st_ldisc_ops->receive_buf = st_tty_receive;
966 st_ldisc_ops->write_wakeup = st_tty_wakeup;
967 st_ldisc_ops->flush_buffer = st_tty_flush_buffer;
968 st_ldisc_ops->owner = THIS_MODULE;
970 err = tty_register_ldisc(N_TI_WL, st_ldisc_ops);
972 pr_err("error registering %d line discipline %ld",
977 pr_debug("registered n_shared line discipline");
979 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
981 pr_err("memory allocation failed");
982 err = tty_unregister_ldisc(N_TI_WL);
984 pr_err("unable to un-register ldisc %ld", err);
990 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
991 * will be pushed in this queue for actual transmission.
993 skb_queue_head_init(&st_gdata->txq);
994 skb_queue_head_init(&st_gdata->tx_waitq);
996 /* Locking used in st_int_enqueue() to avoid multiple execution */
997 spin_lock_init(&st_gdata->lock);
999 /* ldisc_ops ref to be only used in __exit of module */
1000 st_gdata->ldisc_ops = st_ldisc_ops;
1003 err = st_kim_init();
1005 pr_err("error during kim initialization(%ld)", err);
1007 err = tty_unregister_ldisc(N_TI_WL);
1009 pr_err("unable to un-register ldisc");
1010 kfree(st_ldisc_ops);
1015 err = st_ll_init(st_gdata);
1017 pr_err("error during st_ll initialization(%ld)", err);
1019 err = tty_unregister_ldisc(N_TI_WL);
1021 pr_err("unable to un-register ldisc");
1022 kfree(st_ldisc_ops);
1025 *core_data = st_gdata;
1029 void st_core_exit(struct st_data_s *st_gdata)
1032 /* internal module cleanup */
1033 err = st_ll_deinit(st_gdata);
1035 pr_err("error during deinit of ST LL %ld", err);
1037 err = st_kim_deinit();
1039 pr_err("error during deinit of ST KIM %ld", err);
1041 if (st_gdata != NULL) {
1042 /* Free ST Tx Qs and skbs */
1043 skb_queue_purge(&st_gdata->txq);
1044 skb_queue_purge(&st_gdata->tx_waitq);
1045 kfree_skb(st_gdata->rx_skb);
1046 kfree_skb(st_gdata->tx_skb);
1047 /* TTY ldisc cleanup */
1048 err = tty_unregister_ldisc(N_TI_WL);
1050 pr_err("unable to un-register ldisc %ld", err);
1051 kfree(st_gdata->ldisc_ops);
1052 /* free the global data pointer */