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>
31 #include "ti_wilink_st.h"
33 /* strings to be used for rfkill entries and by
34 * ST Core to be used for sysfs debug entry
36 #define PROTO_ENTRY(type, name) name
37 const unsigned char *protocol_strngs[] = {
38 PROTO_ENTRY(ST_BT, "Bluetooth"),
39 PROTO_ENTRY(ST_FM, "FM"),
40 PROTO_ENTRY(ST_GPS, "GPS"),
42 /* function pointer pointing to either,
43 * st_kim_recv during registration to receive fw download responses
44 * st_int_recv after registration to receive proto stack responses
46 void (*st_recv) (void*, const unsigned char*, long);
48 /********************************************************************/
50 /* internal misc functions */
51 bool is_protocol_list_empty(void)
54 pr_debug(" %s ", __func__);
55 for (i = 0; i < ST_MAX; i++) {
56 if (st_gdata->list[i] != NULL)
65 /* can be called in from
66 * -- KIM (during fw download)
67 * -- ST Core (during st_write)
69 * This is the internal write function - a wrapper
72 int st_int_write(struct st_data_s *st_gdata,
73 const unsigned char *data, int count)
75 struct tty_struct *tty;
76 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
77 pr_err("tty unavailable to perform write");
82 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
83 16, 1, data, count, 0);
85 return tty->ops->write(tty, data, count);
90 * push the skb received to relevant
93 void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
95 pr_info(" %s(prot:%d) ", __func__, protoid);
98 (st_gdata == NULL || st_gdata->rx_skb == NULL
99 || st_gdata->list[protoid] == NULL)) {
100 pr_err("protocol %d not registered, no data to send?",
102 kfree_skb(st_gdata->rx_skb);
106 * this shouldn't take long
107 * - should be just skb_queue_tail for the
108 * protocol stack driver
110 if (likely(st_gdata->list[protoid]->recv != NULL)) {
112 (st_gdata->list[protoid]->recv
113 (st_gdata->list[protoid]->priv_data, st_gdata->rx_skb)
115 pr_err(" proto stack %d's ->recv failed", protoid);
116 kfree_skb(st_gdata->rx_skb);
120 pr_err(" proto stack %d's ->recv null", protoid);
121 kfree_skb(st_gdata->rx_skb);
128 * to call registration complete callbacks
129 * of all protocol stack drivers
131 void st_reg_complete(struct st_data_s *st_gdata, char err)
134 pr_info(" %s ", __func__);
135 for (i = 0; i < ST_MAX; i++) {
136 if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
137 st_gdata->list[i]->reg_complete_cb != NULL))
138 st_gdata->list[i]->reg_complete_cb
139 (st_gdata->list[i]->priv_data, err);
143 static inline int st_check_data_len(struct st_data_s *st_gdata,
144 int protoid, int len)
146 register int room = skb_tailroom(st_gdata->rx_skb);
148 pr_debug("len %d room %d", len, room);
151 /* Received packet has only packet header and
152 * has zero length payload. So, ask ST CORE to
153 * forward the packet to protocol driver (BT/FM/GPS)
155 st_send_frame(protoid, st_gdata);
157 } else if (len > room) {
158 /* Received packet's payload length is larger.
159 * We can't accommodate it in created skb.
161 pr_err("Data length is too large len %d room %d", len,
163 kfree_skb(st_gdata->rx_skb);
165 /* Packet header has non-zero payload length and
166 * we have enough space in created skb. Lets read
168 st_gdata->rx_state = ST_BT_W4_DATA;
169 st_gdata->rx_count = len;
173 /* Change ST state to continue to process next
175 st_gdata->rx_state = ST_W4_PACKET_TYPE;
176 st_gdata->rx_skb = NULL;
177 st_gdata->rx_count = 0;
183 * st_wakeup_ack - internal function for action when wake-up ack
186 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
189 register struct sk_buff *waiting_skb;
190 unsigned long flags = 0;
192 spin_lock_irqsave(&st_gdata->lock, flags);
193 /* de-Q from waitQ and Q in txQ now that the
196 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
197 skb_queue_tail(&st_gdata->txq, waiting_skb);
199 /* state forwarded to ST LL */
200 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
201 spin_unlock_irqrestore(&st_gdata->lock, flags);
203 /* wake up to send the recently copied skbs from waitQ */
204 st_tx_wakeup(st_gdata);
208 * st_int_recv - ST's internal receive function.
209 * Decodes received RAW data and forwards to corresponding
210 * client drivers (Bluetooth,FM,GPS..etc).
211 * This can receive various types of packets,
212 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
213 * CH-8 packets from FM, CH-9 packets from GPS cores.
215 void st_int_recv(void *disc_data,
216 const unsigned char *data, long count)
219 struct hci_event_hdr *eh;
220 struct hci_acl_hdr *ah;
221 struct hci_sco_hdr *sh;
222 struct fm_event_hdr *fm;
223 struct gps_event_hdr *gps;
224 register int len = 0, type = 0, dlen = 0;
225 static enum proto_type protoid = ST_MAX;
226 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
229 /* tty_receive sent null ? */
230 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
231 pr_err(" received null from TTY ");
235 pr_info("count %ld rx_state %ld"
236 "rx_count %ld", count, st_gdata->rx_state,
239 /* Decode received bytes here */
241 if (st_gdata->rx_count) {
242 len = min_t(unsigned int, st_gdata->rx_count, count);
243 memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
244 st_gdata->rx_count -= len;
248 if (st_gdata->rx_count)
251 /* Check ST RX state machine , where are we? */
252 switch (st_gdata->rx_state) {
254 /* Waiting for complete packet ? */
256 pr_debug("Complete pkt received");
258 /* Ask ST CORE to forward
259 * the packet to protocol driver */
260 st_send_frame(protoid, st_gdata);
262 st_gdata->rx_state = ST_W4_PACKET_TYPE;
263 st_gdata->rx_skb = NULL;
264 protoid = ST_MAX; /* is this required ? */
267 /* Waiting for Bluetooth event header ? */
268 case ST_BT_W4_EVENT_HDR:
269 eh = (struct hci_event_hdr *)st_gdata->rx_skb->
272 pr_debug("Event header: evt 0x%2.2x"
273 "plen %d", eh->evt, eh->plen);
275 st_check_data_len(st_gdata, protoid, eh->plen);
278 /* Waiting for Bluetooth acl header ? */
279 case ST_BT_W4_ACL_HDR:
280 ah = (struct hci_acl_hdr *)st_gdata->rx_skb->
282 dlen = __le16_to_cpu(ah->dlen);
284 pr_info("ACL header: dlen %d", dlen);
286 st_check_data_len(st_gdata, protoid, dlen);
289 /* Waiting for Bluetooth sco header ? */
290 case ST_BT_W4_SCO_HDR:
291 sh = (struct hci_sco_hdr *)st_gdata->rx_skb->
294 pr_info("SCO header: dlen %d", sh->dlen);
296 st_check_data_len(st_gdata, protoid, sh->dlen);
298 case ST_FM_W4_EVENT_HDR:
299 fm = (struct fm_event_hdr *)st_gdata->rx_skb->
301 pr_info("FM Header: ");
302 st_check_data_len(st_gdata, ST_FM, fm->plen);
304 /* TODO : Add GPS packet machine logic here */
305 case ST_GPS_W4_EVENT_HDR:
306 /* [0x09 pkt hdr][R/W byte][2 byte len] */
307 gps = (struct gps_event_hdr *)st_gdata->rx_skb->
309 pr_info("GPS Header: ");
310 st_check_data_len(st_gdata, ST_GPS, gps->plen);
312 } /* end of switch rx_state */
315 /* end of if rx_count */
316 /* Check first byte of packet and identify module
317 * owner (BT/FM/GPS) */
320 /* Bluetooth event packet? */
322 pr_info("Event packet");
323 st_gdata->rx_state = ST_BT_W4_EVENT_HDR;
324 st_gdata->rx_count = HCI_EVENT_HDR_SIZE;
325 type = HCI_EVENT_PKT;
329 /* Bluetooth acl packet? */
330 case HCI_ACLDATA_PKT:
331 pr_info("ACL packet");
332 st_gdata->rx_state = ST_BT_W4_ACL_HDR;
333 st_gdata->rx_count = HCI_ACL_HDR_SIZE;
334 type = HCI_ACLDATA_PKT;
338 /* Bluetooth sco packet? */
339 case HCI_SCODATA_PKT:
340 pr_info("SCO packet");
341 st_gdata->rx_state = ST_BT_W4_SCO_HDR;
342 st_gdata->rx_count = HCI_SCO_HDR_SIZE;
343 type = HCI_SCODATA_PKT;
347 /* Channel 8(FM) packet? */
349 pr_info("FM CH8 packet");
350 type = ST_FM_CH8_PKT;
351 st_gdata->rx_state = ST_FM_W4_EVENT_HDR;
352 st_gdata->rx_count = FM_EVENT_HDR_SIZE;
356 /* Channel 9(GPS) packet? */
357 case 0x9: /*ST_LL_GPS_CH9_PKT */
358 pr_info("GPS CH9 packet");
359 type = 0x9; /* ST_LL_GPS_CH9_PKT; */
361 st_gdata->rx_state = ST_GPS_W4_EVENT_HDR;
362 st_gdata->rx_count = 3; /* GPS_EVENT_HDR_SIZE -1*/
367 pr_info("PM packet");
368 /* this takes appropriate action based on
369 * sleep state received --
371 st_ll_sleep_state(st_gdata, *ptr);
376 pr_info("PM packet");
377 /* wake up ack received */
378 st_wakeup_ack(st_gdata, *ptr);
384 pr_err("Unknown packet type %2.2x", (__u8) *ptr);
394 /* Allocate new packet to hold received data */
396 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
397 if (!st_gdata->rx_skb) {
398 pr_err("Can't allocate mem for new packet");
399 st_gdata->rx_state = ST_W4_PACKET_TYPE;
400 st_gdata->rx_count = 0;
403 bt_cb(st_gdata->rx_skb)->pkt_type = type;
405 case ST_FM: /* for FM */
407 alloc_skb(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
408 if (!st_gdata->rx_skb) {
409 pr_err("Can't allocate mem for new packet");
410 st_gdata->rx_state = ST_W4_PACKET_TYPE;
411 st_gdata->rx_count = 0;
414 /* place holder 0x08 */
415 skb_reserve(st_gdata->rx_skb, 1);
416 st_gdata->rx_skb->cb[0] = ST_FM_CH8_PKT;
421 alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC);
422 if (!st_gdata->rx_skb) {
423 pr_err("Can't allocate mem for new packet");
424 st_gdata->rx_state = ST_W4_PACKET_TYPE;
425 st_gdata->rx_count = 0;
428 /* place holder 0x09 */
429 skb_reserve(st_gdata->rx_skb, 1);
430 st_gdata->rx_skb->cb[0] = 0x09; /*ST_GPS_CH9_PKT; */
436 pr_debug("done %s", __func__);
441 * st_int_dequeue - internal de-Q function.
442 * If the previous data set was not written
443 * completely, return that skb which has the pending data.
444 * In normal cases, return top of txq.
446 struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
448 struct sk_buff *returning_skb;
450 pr_debug("%s", __func__);
451 if (st_gdata->tx_skb != NULL) {
452 returning_skb = st_gdata->tx_skb;
453 st_gdata->tx_skb = NULL;
454 return returning_skb;
456 return skb_dequeue(&st_gdata->txq);
460 * st_int_enqueue - internal Q-ing function.
461 * Will either Q the skb to txq or the tx_waitq
462 * depending on the ST LL state.
463 * If the chip is asleep, then Q it onto waitq and
465 * txq and waitq needs protection since the other contexts
466 * may be sending data, waking up chip.
468 void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
470 unsigned long flags = 0;
472 pr_debug("%s", __func__);
473 spin_lock_irqsave(&st_gdata->lock, flags);
475 switch (st_ll_getstate(st_gdata)) {
477 pr_info("ST LL is AWAKE, sending normally");
478 skb_queue_tail(&st_gdata->txq, skb);
480 case ST_LL_ASLEEP_TO_AWAKE:
481 skb_queue_tail(&st_gdata->tx_waitq, skb);
483 case ST_LL_AWAKE_TO_ASLEEP:
484 pr_err("ST LL is illegal state(%ld),"
485 "purging received skb.", st_ll_getstate(st_gdata));
489 skb_queue_tail(&st_gdata->tx_waitq, skb);
490 st_ll_wakeup(st_gdata);
493 pr_err("ST LL is illegal state(%ld),"
494 "purging received skb.", st_ll_getstate(st_gdata));
499 spin_unlock_irqrestore(&st_gdata->lock, flags);
500 pr_debug("done %s", __func__);
505 * internal wakeup function
507 * - TTY layer when write's finished
508 * - st_write (in context of the protocol stack)
510 void st_tx_wakeup(struct st_data_s *st_data)
513 unsigned long flags; /* for irq save flags */
514 pr_debug("%s", __func__);
515 /* check for sending & set flag sending here */
516 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
517 pr_info("ST already sending");
519 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
521 /* TX_WAKEUP will be checked in another
525 do { /* come back if st_tx_wakeup is set */
526 /* woke-up to write */
527 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
528 while ((skb = st_int_dequeue(st_data))) {
530 spin_lock_irqsave(&st_data->lock, flags);
531 /* enable wake-up from TTY */
532 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
533 len = st_int_write(st_data, skb->data, skb->len);
535 /* if skb->len = len as expected, skb->len=0 */
537 /* would be the next skb to be sent */
538 st_data->tx_skb = skb;
539 spin_unlock_irqrestore(&st_data->lock, flags);
543 spin_unlock_irqrestore(&st_data->lock, flags);
545 /* if wake-up is set in another context- restart sending */
546 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
548 /* clear flag sending */
549 clear_bit(ST_TX_SENDING, &st_data->tx_state);
552 /********************************************************************/
553 /* functions called from ST KIM
555 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
557 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
558 st_gdata->protos_registered,
559 st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
560 st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
561 st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
564 /********************************************************************/
566 * functions called from protocol stack drivers
569 long st_register(struct st_proto_s *new_proto)
571 struct st_data_s *st_gdata;
573 unsigned long flags = 0;
575 st_kim_ref(&st_gdata, 0);
576 pr_info("%s(%d) ", __func__, new_proto->type);
577 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
578 || new_proto->reg_complete_cb == NULL) {
579 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
583 if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
584 pr_err("protocol %d not supported", new_proto->type);
585 return -EPROTONOSUPPORT;
588 if (st_gdata->list[new_proto->type] != NULL) {
589 pr_err("protocol %d already registered", new_proto->type);
593 /* can be from process context only */
594 spin_lock_irqsave(&st_gdata->lock, flags);
596 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
597 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
598 /* fw download in progress */
599 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
601 st_gdata->list[new_proto->type] = new_proto;
602 st_gdata->protos_registered++;
603 new_proto->write = st_write;
605 set_bit(ST_REG_PENDING, &st_gdata->st_state);
606 spin_unlock_irqrestore(&st_gdata->lock, flags);
608 } else if (st_gdata->protos_registered == ST_EMPTY) {
609 pr_info(" protocol list empty :%d ", new_proto->type);
610 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
611 st_recv = st_kim_recv;
613 /* release lock previously held - re-locked below */
614 spin_unlock_irqrestore(&st_gdata->lock, flags);
616 /* enable the ST LL - to set default chip state */
617 st_ll_enable(st_gdata);
618 /* this may take a while to complete
619 * since it involves BT fw download
621 err = st_kim_start(st_gdata->kim_data);
623 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
624 if ((st_gdata->protos_registered != ST_EMPTY) &&
625 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
626 pr_err(" KIM failure complete callback ");
627 st_reg_complete(st_gdata, -1);
633 /* the protocol might require other gpios to be toggled
635 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
637 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
638 st_recv = st_int_recv;
640 /* this is where all pending registration
641 * are signalled to be complete by calling callback functions
643 if ((st_gdata->protos_registered != ST_EMPTY) &&
644 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
645 pr_debug(" call reg complete callback ");
646 st_reg_complete(st_gdata, 0);
648 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
650 /* check for already registered once more,
651 * since the above check is old
653 if (st_gdata->list[new_proto->type] != NULL) {
654 pr_err(" proto %d already registered ",
659 spin_lock_irqsave(&st_gdata->lock, flags);
660 st_gdata->list[new_proto->type] = new_proto;
661 st_gdata->protos_registered++;
662 new_proto->write = st_write;
663 spin_unlock_irqrestore(&st_gdata->lock, flags);
666 /* if fw is already downloaded & new stack registers protocol */
668 switch (new_proto->type) {
674 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
678 pr_err("%d protocol not supported",
680 spin_unlock_irqrestore(&st_gdata->lock, flags);
681 return -EPROTONOSUPPORT;
683 st_gdata->list[new_proto->type] = new_proto;
684 st_gdata->protos_registered++;
685 new_proto->write = st_write;
687 /* lock already held before entering else */
688 spin_unlock_irqrestore(&st_gdata->lock, flags);
691 pr_debug("done %s(%d) ", __func__, new_proto->type);
693 EXPORT_SYMBOL_GPL(st_register);
695 /* to unregister a protocol -
696 * to be called from protocol stack driver
698 long st_unregister(enum proto_type type)
701 unsigned long flags = 0;
702 struct st_data_s *st_gdata;
704 pr_debug("%s: %d ", __func__, type);
706 st_kim_ref(&st_gdata, 0);
707 if (type < ST_BT || type >= ST_MAX) {
708 pr_err(" protocol %d not supported", type);
709 return -EPROTONOSUPPORT;
712 spin_lock_irqsave(&st_gdata->lock, flags);
714 if (st_gdata->list[type] == NULL) {
715 pr_err(" protocol %d not registered", type);
716 spin_unlock_irqrestore(&st_gdata->lock, flags);
717 return -EPROTONOSUPPORT;
720 st_gdata->protos_registered--;
721 st_gdata->list[type] = NULL;
723 /* kim ignores BT in the below function
724 * and handles the rest, BT is toggled
725 * only in kim_start and kim_stop
727 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
728 spin_unlock_irqrestore(&st_gdata->lock, flags);
730 if ((st_gdata->protos_registered == ST_EMPTY) &&
731 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
732 pr_info(" all protocols unregistered ");
734 /* stop traffic on tty */
736 tty_ldisc_flush(st_gdata->tty);
737 stop_tty(st_gdata->tty);
740 /* all protocols now unregistered */
741 st_kim_stop(st_gdata->kim_data);
743 st_ll_disable(st_gdata);
749 * called in protocol stack drivers
750 * via the write function pointer
752 long st_write(struct sk_buff *skb)
754 struct st_data_s *st_gdata;
756 enum proto_type protoid = ST_MAX;
760 st_kim_ref(&st_gdata, 0);
761 if (unlikely(skb == NULL || st_gdata == NULL
762 || st_gdata->tty == NULL)) {
763 pr_err("data/tty unavailable to perform write");
766 #ifdef DEBUG /* open-up skb to read the 1st byte */
767 switch (skb->data[0]) {
768 case HCI_COMMAND_PKT:
769 case HCI_ACLDATA_PKT:
770 case HCI_SCODATA_PKT:
780 if (unlikely(st_gdata->list[protoid] == NULL)) {
781 pr_err(" protocol %d not registered, and writing? ",
786 pr_debug("%d to be written", skb->len);
789 /* st_ll to decide where to enqueue the skb */
790 st_int_enqueue(st_gdata, skb);
792 st_tx_wakeup(st_gdata);
794 /* return number of bytes written */
798 /* for protocols making use of shared transport */
799 EXPORT_SYMBOL_GPL(st_unregister);
801 /********************************************************************/
803 * functions called from TTY layer
805 static int st_tty_open(struct tty_struct *tty)
808 struct st_data_s *st_gdata;
809 pr_info("%s ", __func__);
811 st_kim_ref(&st_gdata, 0);
813 tty->disc_data = st_gdata;
815 /* don't do an wakeup for now */
816 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
818 /* mem already allocated
820 tty->receive_room = 65536;
821 /* Flush any pending characters in the driver and discipline. */
822 tty_ldisc_flush(tty);
823 tty_driver_flush_buffer(tty);
825 * signal to UIM via KIM that -
826 * installation of N_TI_WL ldisc is complete
828 st_kim_complete(st_gdata->kim_data);
829 pr_debug("done %s", __func__);
833 static void st_tty_close(struct tty_struct *tty)
835 unsigned char i = ST_MAX;
836 unsigned long flags = 0;
837 struct st_data_s *st_gdata = tty->disc_data;
839 pr_info("%s ", __func__);
842 * if a protocol has been registered & line discipline
843 * un-installed for some reason - what should be done ?
845 spin_lock_irqsave(&st_gdata->lock, flags);
846 for (i = ST_BT; i < ST_MAX; i++) {
847 if (st_gdata->list[i] != NULL)
848 pr_err("%d not un-registered", i);
849 st_gdata->list[i] = NULL;
851 st_gdata->protos_registered = 0;
852 spin_unlock_irqrestore(&st_gdata->lock, flags);
854 * signal to UIM via KIM that -
855 * N_TI_WL ldisc is un-installed
857 st_kim_complete(st_gdata->kim_data);
858 st_gdata->tty = NULL;
859 /* Flush any pending characters in the driver and discipline. */
860 tty_ldisc_flush(tty);
861 tty_driver_flush_buffer(tty);
863 spin_lock_irqsave(&st_gdata->lock, flags);
864 /* empty out txq and tx_waitq */
865 skb_queue_purge(&st_gdata->txq);
866 skb_queue_purge(&st_gdata->tx_waitq);
867 /* reset the TTY Rx states of ST */
868 st_gdata->rx_count = 0;
869 st_gdata->rx_state = ST_W4_PACKET_TYPE;
870 kfree_skb(st_gdata->rx_skb);
871 st_gdata->rx_skb = NULL;
872 spin_unlock_irqrestore(&st_gdata->lock, flags);
874 pr_debug("%s: done ", __func__);
877 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
878 char *tty_flags, int count)
882 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
883 16, 1, data, count, 0);
887 * if fw download is in progress then route incoming data
888 * to KIM for validation
890 st_recv(tty->disc_data, data, count);
891 pr_debug("done %s", __func__);
894 /* wake-up function called in from the TTY layer
895 * inside the internal wakeup function will be called
897 static void st_tty_wakeup(struct tty_struct *tty)
899 struct st_data_s *st_gdata = tty->disc_data;
900 pr_debug("%s ", __func__);
901 /* don't do an wakeup for now */
902 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
904 /* call our internal wakeup */
905 st_tx_wakeup((void *)st_gdata);
908 static void st_tty_flush_buffer(struct tty_struct *tty)
910 struct st_data_s *st_gdata = tty->disc_data;
911 pr_debug("%s ", __func__);
913 kfree_skb(st_gdata->tx_skb);
914 st_gdata->tx_skb = NULL;
916 tty->ops->flush_buffer(tty);
920 /********************************************************************/
921 int st_core_init(struct st_data_s **core_data)
923 struct st_data_s *st_gdata;
925 static struct tty_ldisc_ops *st_ldisc_ops;
927 /* populate and register to TTY line discipline */
928 st_ldisc_ops = kzalloc(sizeof(*st_ldisc_ops), GFP_KERNEL);
930 pr_err("no mem to allocate");
934 st_ldisc_ops->magic = TTY_LDISC_MAGIC;
935 st_ldisc_ops->name = "n_st"; /*"n_hci"; */
936 st_ldisc_ops->open = st_tty_open;
937 st_ldisc_ops->close = st_tty_close;
938 st_ldisc_ops->receive_buf = st_tty_receive;
939 st_ldisc_ops->write_wakeup = st_tty_wakeup;
940 st_ldisc_ops->flush_buffer = st_tty_flush_buffer;
941 st_ldisc_ops->owner = THIS_MODULE;
943 err = tty_register_ldisc(N_TI_WL, st_ldisc_ops);
945 pr_err("error registering %d line discipline %ld",
950 pr_debug("registered n_shared line discipline");
952 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
954 pr_err("memory allocation failed");
955 err = tty_unregister_ldisc(N_TI_WL);
957 pr_err("unable to un-register ldisc %ld", err);
963 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
964 * will be pushed in this queue for actual transmission.
966 skb_queue_head_init(&st_gdata->txq);
967 skb_queue_head_init(&st_gdata->tx_waitq);
969 /* Locking used in st_int_enqueue() to avoid multiple execution */
970 spin_lock_init(&st_gdata->lock);
972 /* ldisc_ops ref to be only used in __exit of module */
973 st_gdata->ldisc_ops = st_ldisc_ops;
978 pr_err("error during kim initialization(%ld)", err);
980 err = tty_unregister_ldisc(N_TI_WL);
982 pr_err("unable to un-register ldisc");
988 err = st_ll_init(st_gdata);
990 pr_err("error during st_ll initialization(%ld)", err);
992 err = tty_unregister_ldisc(N_TI_WL);
994 pr_err("unable to un-register ldisc");
998 *core_data = st_gdata;
1002 void st_core_exit(struct st_data_s *st_gdata)
1005 /* internal module cleanup */
1006 err = st_ll_deinit(st_gdata);
1008 pr_err("error during deinit of ST LL %ld", err);
1010 err = st_kim_deinit();
1012 pr_err("error during deinit of ST KIM %ld", err);
1014 if (st_gdata != NULL) {
1015 /* Free ST Tx Qs and skbs */
1016 skb_queue_purge(&st_gdata->txq);
1017 skb_queue_purge(&st_gdata->tx_waitq);
1018 kfree_skb(st_gdata->rx_skb);
1019 kfree_skb(st_gdata->tx_skb);
1020 /* TTY ldisc cleanup */
1021 err = tty_unregister_ldisc(N_TI_WL);
1023 pr_err("unable to un-register ldisc %ld", err);
1024 kfree(st_gdata->ldisc_ops);
1025 /* free the global data pointer */