1 // SPDX-License-Identifier: GPL-2.0-only
3 * Shared Transport Line discipline driver Core
4 * This hooks up ST KIM driver and ST LL driver
5 * Copyright (C) 2009-2010 Texas Instruments
6 * Author: Pavan Savoy <pavan_savoy@ti.com>
9 #define pr_fmt(fmt) "(stc): " fmt
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/tty.h>
14 #include <linux/seq_file.h>
15 #include <linux/skbuff.h>
17 #include <linux/ti_wilink_st.h>
18 #include <linux/netdevice.h>
20 extern void st_kim_recv(void *, const unsigned char *, long);
21 void st_int_recv(void *, const unsigned char *, long);
23 * function pointer pointing to either,
24 * st_kim_recv during registration to receive fw download responses
25 * st_int_recv after registration to receive proto stack responses
27 static void (*st_recv) (void *, const unsigned char *, long);
29 /********************************************************************/
30 static void add_channel_to_table(struct st_data_s *st_gdata,
31 struct st_proto_s *new_proto)
33 pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
34 /* list now has the channel id as index itself */
35 st_gdata->list[new_proto->chnl_id] = new_proto;
36 st_gdata->is_registered[new_proto->chnl_id] = true;
39 static void remove_channel_from_table(struct st_data_s *st_gdata,
40 struct st_proto_s *proto)
42 pr_info("%s: id %d\n", __func__, proto->chnl_id);
43 /* st_gdata->list[proto->chnl_id] = NULL; */
44 st_gdata->is_registered[proto->chnl_id] = false;
48 * called from KIM during firmware download.
50 * This is a wrapper function to tty->ops->write_room.
51 * It returns number of free space available in
54 int st_get_uart_wr_room(struct st_data_s *st_gdata)
56 struct tty_struct *tty;
57 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
58 pr_err("tty unavailable to perform write");
62 return tty->ops->write_room(tty);
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 static void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
96 pr_debug(" %s(prot:%d) ", __func__, chnl_id);
99 (st_gdata == NULL || st_gdata->rx_skb == NULL
100 || st_gdata->is_registered[chnl_id] == false)) {
101 pr_err("chnl_id %d not registered, no data to send?",
103 kfree_skb(st_gdata->rx_skb);
108 * this shouldn't take long
109 * - should be just skb_queue_tail for the
110 * protocol stack driver
112 if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
114 (st_gdata->list[chnl_id]->recv
115 (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
117 pr_err(" proto stack %d's ->recv failed", chnl_id);
118 kfree_skb(st_gdata->rx_skb);
122 pr_err(" proto stack %d's ->recv null", chnl_id);
123 kfree_skb(st_gdata->rx_skb);
129 * st_reg_complete - to call registration complete callbacks
130 * of all protocol stack drivers
131 * This function is being called with spin lock held, protocol drivers are
132 * only expected to complete their waits and do nothing more than that.
134 static void st_reg_complete(struct st_data_s *st_gdata, int err)
137 pr_info(" %s ", __func__);
138 for (i = 0; i < ST_MAX_CHANNELS; i++) {
139 if (likely(st_gdata != NULL &&
140 st_gdata->is_registered[i] == true &&
141 st_gdata->list[i]->reg_complete_cb != NULL)) {
142 st_gdata->list[i]->reg_complete_cb
143 (st_gdata->list[i]->priv_data, err);
144 pr_info("protocol %d's cb sent %d\n", i, err);
145 if (err) { /* cleanup registered protocol */
146 st_gdata->is_registered[i] = false;
147 if (st_gdata->protos_registered)
148 st_gdata->protos_registered--;
154 static inline int st_check_data_len(struct st_data_s *st_gdata,
155 unsigned char chnl_id, int len)
157 int room = skb_tailroom(st_gdata->rx_skb);
159 pr_debug("len %d room %d", len, room);
163 * Received packet has only packet header and
164 * has zero length payload. So, ask ST CORE to
165 * forward the packet to protocol driver (BT/FM/GPS)
167 st_send_frame(chnl_id, st_gdata);
169 } else if (len > room) {
171 * Received packet's payload length is larger.
172 * We can't accommodate it in created skb.
174 pr_err("Data length is too large len %d room %d", len,
176 kfree_skb(st_gdata->rx_skb);
179 * Packet header has non-zero payload length and
180 * we have enough space in created skb. Lets read
182 st_gdata->rx_state = ST_W4_DATA;
183 st_gdata->rx_count = len;
187 /* Change ST state to continue to process next packet */
188 st_gdata->rx_state = ST_W4_PACKET_TYPE;
189 st_gdata->rx_skb = NULL;
190 st_gdata->rx_count = 0;
191 st_gdata->rx_chnl = 0;
197 * st_wakeup_ack - internal function for action when wake-up ack
200 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
203 struct sk_buff *waiting_skb;
204 unsigned long flags = 0;
206 spin_lock_irqsave(&st_gdata->lock, flags);
208 * de-Q from waitQ and Q in txQ now that the
211 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
212 skb_queue_tail(&st_gdata->txq, waiting_skb);
214 /* state forwarded to ST LL */
215 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
216 spin_unlock_irqrestore(&st_gdata->lock, flags);
218 /* wake up to send the recently copied skbs from waitQ */
219 st_tx_wakeup(st_gdata);
223 * st_int_recv - ST's internal receive function.
224 * Decodes received RAW data and forwards to corresponding
225 * client drivers (Bluetooth,FM,GPS..etc).
226 * This can receive various types of packets,
227 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
228 * CH-8 packets from FM, CH-9 packets from GPS cores.
230 void st_int_recv(void *disc_data,
231 const unsigned char *data, long count)
234 struct st_proto_s *proto;
235 unsigned short payload_len = 0;
237 unsigned char type = 0;
239 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
243 /* tty_receive sent null ? */
244 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
245 pr_err(" received null from TTY ");
249 pr_debug("count %ld rx_state %ld"
250 "rx_count %ld", count, st_gdata->rx_state,
253 spin_lock_irqsave(&st_gdata->lock, flags);
254 /* Decode received bytes here */
256 if (st_gdata->rx_count) {
257 len = min_t(unsigned int, st_gdata->rx_count, count);
258 skb_put_data(st_gdata->rx_skb, ptr, len);
259 st_gdata->rx_count -= len;
263 if (st_gdata->rx_count)
266 /* Check ST RX state machine , where are we? */
267 switch (st_gdata->rx_state) {
268 /* Waiting for complete packet ? */
270 pr_debug("Complete pkt received");
272 * Ask ST CORE to forward
273 * the packet to protocol driver
275 st_send_frame(st_gdata->rx_chnl, st_gdata);
277 st_gdata->rx_state = ST_W4_PACKET_TYPE;
278 st_gdata->rx_skb = NULL;
280 /* parse the header to know details */
282 proto = st_gdata->list[st_gdata->rx_chnl];
284 &st_gdata->rx_skb->data
285 [proto->offset_len_in_hdr];
286 pr_debug("plen pointing to %x\n", *plen);
287 if (proto->len_size == 1) /* 1 byte len field */
288 payload_len = *(unsigned char *)plen;
289 else if (proto->len_size == 2)
291 __le16_to_cpu(*(unsigned short *)plen);
293 pr_info("%s: invalid length "
295 __func__, proto->chnl_id);
296 st_check_data_len(st_gdata, proto->chnl_id,
298 pr_debug("off %d, pay len %d\n",
299 proto->offset_len_in_hdr, payload_len);
301 } /* end of switch rx_state */
304 /* end of if rx_count */
307 * Check first byte of packet and identify module
314 pr_debug("PM packet");
316 * this takes appropriate action based on
317 * sleep state received --
319 st_ll_sleep_state(st_gdata, *ptr);
321 * if WAKEUP_IND collides copy from waitq to txq
322 * and assume chip awake
324 spin_unlock_irqrestore(&st_gdata->lock, flags);
325 if (st_ll_getstate(st_gdata) == ST_LL_AWAKE)
326 st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK);
327 spin_lock_irqsave(&st_gdata->lock, flags);
333 pr_debug("PM packet");
335 spin_unlock_irqrestore(&st_gdata->lock, flags);
336 /* wake up ack received */
337 st_wakeup_ack(st_gdata, *ptr);
338 spin_lock_irqsave(&st_gdata->lock, flags);
348 * Default case means non-HCILL packets,
349 * possibilities are packets for:
350 * (a) valid protocol - Supported Protocols within
351 * the ST_MAX_CHANNELS.
352 * (b) registered protocol - Checked by
353 * "st_gdata->list[type] == NULL)" are supported
355 * Rules out any invalid protocol and
356 * unregistered protocols with channel ID < 16.
359 if ((type >= ST_MAX_CHANNELS) ||
360 (st_gdata->list[type] == NULL)) {
361 pr_err("chip/interface misbehavior: "
362 "dropping frame starting "
363 "with 0x%02x\n", type);
367 st_gdata->rx_skb = alloc_skb(
368 st_gdata->list[type]->max_frame_size,
370 if (st_gdata->rx_skb == NULL) {
371 pr_err("out of memory: dropping\n");
375 skb_reserve(st_gdata->rx_skb,
376 st_gdata->list[type]->reserve);
377 /* next 2 required for BT only */
378 st_gdata->rx_skb->cb[0] = type; /*pkt_type*/
379 st_gdata->rx_skb->cb[1] = 0; /*incoming*/
380 st_gdata->rx_chnl = *ptr;
381 st_gdata->rx_state = ST_W4_HEADER;
382 st_gdata->rx_count = st_gdata->list[type]->hdr_len;
383 pr_debug("rx_count %ld\n", st_gdata->rx_count);
389 spin_unlock_irqrestore(&st_gdata->lock, flags);
390 pr_debug("done %s", __func__);
395 * st_int_dequeue - internal de-Q function.
396 * If the previous data set was not written
397 * completely, return that skb which has the pending data.
398 * In normal cases, return top of txq.
400 static struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
402 struct sk_buff *returning_skb;
404 pr_debug("%s", __func__);
405 if (st_gdata->tx_skb != NULL) {
406 returning_skb = st_gdata->tx_skb;
407 st_gdata->tx_skb = NULL;
408 return returning_skb;
410 return skb_dequeue(&st_gdata->txq);
414 * st_int_enqueue - internal Q-ing function.
415 * Will either Q the skb to txq or the tx_waitq
416 * depending on the ST LL state.
417 * If the chip is asleep, then Q it onto waitq and
419 * txq and waitq needs protection since the other contexts
420 * may be sending data, waking up chip.
422 static void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
424 unsigned long flags = 0;
426 pr_debug("%s", __func__);
427 spin_lock_irqsave(&st_gdata->lock, flags);
429 switch (st_ll_getstate(st_gdata)) {
431 pr_debug("ST LL is AWAKE, sending normally");
432 skb_queue_tail(&st_gdata->txq, skb);
434 case ST_LL_ASLEEP_TO_AWAKE:
435 skb_queue_tail(&st_gdata->tx_waitq, skb);
437 case ST_LL_AWAKE_TO_ASLEEP:
438 pr_err("ST LL is illegal state(%ld),"
439 "purging received skb.", st_ll_getstate(st_gdata));
440 dev_kfree_skb_irq(skb);
443 skb_queue_tail(&st_gdata->tx_waitq, skb);
444 st_ll_wakeup(st_gdata);
447 pr_err("ST LL is illegal state(%ld),"
448 "purging received skb.", st_ll_getstate(st_gdata));
449 dev_kfree_skb_irq(skb);
453 spin_unlock_irqrestore(&st_gdata->lock, flags);
454 pr_debug("done %s", __func__);
459 * internal wakeup function
461 * - TTY layer when write's finished
462 * - st_write (in context of the protocol stack)
464 static void work_fn_write_wakeup(struct work_struct *work)
466 struct st_data_s *st_gdata = container_of(work, struct st_data_s,
469 st_tx_wakeup((void *)st_gdata);
471 void st_tx_wakeup(struct st_data_s *st_data)
474 unsigned long flags; /* for irq save flags */
475 pr_debug("%s", __func__);
476 /* check for sending & set flag sending here */
477 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
478 pr_debug("ST already sending");
480 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
482 /* TX_WAKEUP will be checked in another
486 do { /* come back if st_tx_wakeup is set */
487 /* woke-up to write */
488 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
489 while ((skb = st_int_dequeue(st_data))) {
491 spin_lock_irqsave(&st_data->lock, flags);
492 /* enable wake-up from TTY */
493 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
494 len = st_int_write(st_data, skb->data, skb->len);
496 /* if skb->len = len as expected, skb->len=0 */
498 /* would be the next skb to be sent */
499 st_data->tx_skb = skb;
500 spin_unlock_irqrestore(&st_data->lock, flags);
503 dev_kfree_skb_irq(skb);
504 spin_unlock_irqrestore(&st_data->lock, flags);
506 /* if wake-up is set in another context- restart sending */
507 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
509 /* clear flag sending */
510 clear_bit(ST_TX_SENDING, &st_data->tx_state);
513 /********************************************************************/
514 /* functions called from ST KIM
516 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
518 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
519 st_gdata->protos_registered,
520 st_gdata->is_registered[0x04] == true ? 'R' : 'U',
521 st_gdata->is_registered[0x08] == true ? 'R' : 'U',
522 st_gdata->is_registered[0x09] == true ? 'R' : 'U');
525 /********************************************************************/
527 * functions called from protocol stack drivers
530 long st_register(struct st_proto_s *new_proto)
532 struct st_data_s *st_gdata;
534 unsigned long flags = 0;
536 st_kim_ref(&st_gdata, 0);
537 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
538 || new_proto->reg_complete_cb == NULL) {
539 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
543 if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
544 pr_err("chnl_id %d not supported", new_proto->chnl_id);
545 return -EPROTONOSUPPORT;
548 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
549 pr_err("chnl_id %d already registered", new_proto->chnl_id);
553 /* can be from process context only */
554 spin_lock_irqsave(&st_gdata->lock, flags);
556 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
557 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
558 /* fw download in progress */
560 add_channel_to_table(st_gdata, new_proto);
561 st_gdata->protos_registered++;
562 new_proto->write = st_write;
564 set_bit(ST_REG_PENDING, &st_gdata->st_state);
565 spin_unlock_irqrestore(&st_gdata->lock, flags);
567 } else if (st_gdata->protos_registered == ST_EMPTY) {
568 pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
569 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
570 st_recv = st_kim_recv;
572 /* enable the ST LL - to set default chip state */
573 st_ll_enable(st_gdata);
575 /* release lock previously held - re-locked below */
576 spin_unlock_irqrestore(&st_gdata->lock, flags);
579 * this may take a while to complete
580 * since it involves BT fw download
582 err = st_kim_start(st_gdata->kim_data);
584 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
585 if ((st_gdata->protos_registered != ST_EMPTY) &&
586 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
587 pr_err(" KIM failure complete callback ");
588 spin_lock_irqsave(&st_gdata->lock, flags);
589 st_reg_complete(st_gdata, err);
590 spin_unlock_irqrestore(&st_gdata->lock, flags);
591 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
596 spin_lock_irqsave(&st_gdata->lock, flags);
598 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
599 st_recv = st_int_recv;
602 * this is where all pending registration
603 * are signalled to be complete by calling callback functions
605 if ((st_gdata->protos_registered != ST_EMPTY) &&
606 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
607 pr_debug(" call reg complete callback ");
608 st_reg_complete(st_gdata, 0);
610 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
613 * check for already registered once more,
614 * since the above check is old
616 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
617 pr_err(" proto %d already registered ",
619 spin_unlock_irqrestore(&st_gdata->lock, flags);
623 add_channel_to_table(st_gdata, new_proto);
624 st_gdata->protos_registered++;
625 new_proto->write = st_write;
626 spin_unlock_irqrestore(&st_gdata->lock, flags);
629 /* if fw is already downloaded & new stack registers protocol */
631 add_channel_to_table(st_gdata, new_proto);
632 st_gdata->protos_registered++;
633 new_proto->write = st_write;
635 /* lock already held before entering else */
636 spin_unlock_irqrestore(&st_gdata->lock, flags);
640 EXPORT_SYMBOL_GPL(st_register);
643 * to unregister a protocol -
644 * to be called from protocol stack driver
646 long st_unregister(struct st_proto_s *proto)
649 unsigned long flags = 0;
650 struct st_data_s *st_gdata;
652 pr_debug("%s: %d ", __func__, proto->chnl_id);
654 st_kim_ref(&st_gdata, 0);
655 if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
656 pr_err(" chnl_id %d not supported", proto->chnl_id);
657 return -EPROTONOSUPPORT;
660 spin_lock_irqsave(&st_gdata->lock, flags);
662 if (st_gdata->is_registered[proto->chnl_id] == false) {
663 pr_err(" chnl_id %d not registered", proto->chnl_id);
664 spin_unlock_irqrestore(&st_gdata->lock, flags);
665 return -EPROTONOSUPPORT;
668 if (st_gdata->protos_registered)
669 st_gdata->protos_registered--;
671 remove_channel_from_table(st_gdata, proto);
672 spin_unlock_irqrestore(&st_gdata->lock, flags);
674 if ((st_gdata->protos_registered == ST_EMPTY) &&
675 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
676 pr_info(" all chnl_ids unregistered ");
678 /* stop traffic on tty */
680 tty_ldisc_flush(st_gdata->tty);
681 stop_tty(st_gdata->tty);
684 /* all chnl_ids now unregistered */
685 st_kim_stop(st_gdata->kim_data);
687 st_ll_disable(st_gdata);
693 * called in protocol stack drivers
694 * via the write function pointer
696 long st_write(struct sk_buff *skb)
698 struct st_data_s *st_gdata;
701 st_kim_ref(&st_gdata, 0);
702 if (unlikely(skb == NULL || st_gdata == NULL
703 || st_gdata->tty == NULL)) {
704 pr_err("data/tty unavailable to perform write");
708 pr_debug("%d to be written", skb->len);
711 /* st_ll to decide where to enqueue the skb */
712 st_int_enqueue(st_gdata, skb);
714 st_tx_wakeup(st_gdata);
716 /* return number of bytes written */
720 /* for protocols making use of shared transport */
721 EXPORT_SYMBOL_GPL(st_unregister);
723 /********************************************************************/
725 * functions called from TTY layer
727 static int st_tty_open(struct tty_struct *tty)
729 struct st_data_s *st_gdata;
730 pr_info("%s ", __func__);
732 st_kim_ref(&st_gdata, 0);
734 tty->disc_data = st_gdata;
736 /* don't do an wakeup for now */
737 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
739 /* mem already allocated
741 tty->receive_room = 65536;
742 /* Flush any pending characters in the driver and discipline. */
743 tty_ldisc_flush(tty);
744 tty_driver_flush_buffer(tty);
746 * signal to UIM via KIM that -
747 * installation of N_TI_WL ldisc is complete
749 st_kim_complete(st_gdata->kim_data);
750 pr_debug("done %s", __func__);
755 static void st_tty_close(struct tty_struct *tty)
759 struct st_data_s *st_gdata = tty->disc_data;
761 pr_info("%s ", __func__);
765 * if a protocol has been registered & line discipline
766 * un-installed for some reason - what should be done ?
768 spin_lock_irqsave(&st_gdata->lock, flags);
769 for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
770 if (st_gdata->is_registered[i] == true)
771 pr_err("%d not un-registered", i);
772 st_gdata->list[i] = NULL;
773 st_gdata->is_registered[i] = false;
775 st_gdata->protos_registered = 0;
776 spin_unlock_irqrestore(&st_gdata->lock, flags);
778 * signal to UIM via KIM that -
779 * N_TI_WL ldisc is un-installed
781 st_kim_complete(st_gdata->kim_data);
782 st_gdata->tty = NULL;
783 /* Flush any pending characters in the driver and discipline. */
784 tty_ldisc_flush(tty);
785 tty_driver_flush_buffer(tty);
787 spin_lock_irqsave(&st_gdata->lock, flags);
788 /* empty out txq and tx_waitq */
789 skb_queue_purge(&st_gdata->txq);
790 skb_queue_purge(&st_gdata->tx_waitq);
791 /* reset the TTY Rx states of ST */
792 st_gdata->rx_count = 0;
793 st_gdata->rx_state = ST_W4_PACKET_TYPE;
794 kfree_skb(st_gdata->rx_skb);
795 st_gdata->rx_skb = NULL;
796 spin_unlock_irqrestore(&st_gdata->lock, flags);
798 pr_debug("%s: done ", __func__);
801 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
802 char *tty_flags, int count)
805 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
806 16, 1, data, count, 0);
810 * if fw download is in progress then route incoming data
811 * to KIM for validation
813 st_recv(tty->disc_data, data, count);
814 pr_debug("done %s", __func__);
818 * wake-up function called in from the TTY layer
819 * inside the internal wakeup function will be called
821 static void st_tty_wakeup(struct tty_struct *tty)
823 struct st_data_s *st_gdata = tty->disc_data;
824 pr_debug("%s ", __func__);
825 /* don't do an wakeup for now */
826 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
829 * schedule the internal wakeup instead of calling directly to
830 * avoid lockup (port->lock needed in tty->ops->write is
833 schedule_work(&st_gdata->work_write_wakeup);
836 static void st_tty_flush_buffer(struct tty_struct *tty)
838 struct st_data_s *st_gdata = tty->disc_data;
839 pr_debug("%s ", __func__);
841 kfree_skb(st_gdata->tx_skb);
842 st_gdata->tx_skb = NULL;
844 tty_driver_flush_buffer(tty);
848 static struct tty_ldisc_ops st_ldisc_ops = {
849 .magic = TTY_LDISC_MAGIC,
852 .close = st_tty_close,
853 .receive_buf = st_tty_receive,
854 .write_wakeup = st_tty_wakeup,
855 .flush_buffer = st_tty_flush_buffer,
859 /********************************************************************/
860 int st_core_init(struct st_data_s **core_data)
862 struct st_data_s *st_gdata;
865 err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops);
867 pr_err("error registering %d line discipline %ld",
871 pr_debug("registered n_shared line discipline");
873 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
875 pr_err("memory allocation failed");
876 err = tty_unregister_ldisc(N_TI_WL);
878 pr_err("unable to un-register ldisc %ld", err);
883 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
884 * will be pushed in this queue for actual transmission.
886 skb_queue_head_init(&st_gdata->txq);
887 skb_queue_head_init(&st_gdata->tx_waitq);
889 /* Locking used in st_int_enqueue() to avoid multiple execution */
890 spin_lock_init(&st_gdata->lock);
892 err = st_ll_init(st_gdata);
894 pr_err("error during st_ll initialization(%ld)", err);
896 err = tty_unregister_ldisc(N_TI_WL);
898 pr_err("unable to un-register ldisc");
902 INIT_WORK(&st_gdata->work_write_wakeup, work_fn_write_wakeup);
904 *core_data = st_gdata;
908 void st_core_exit(struct st_data_s *st_gdata)
911 /* internal module cleanup */
912 err = st_ll_deinit(st_gdata);
914 pr_err("error during deinit of ST LL %ld", err);
916 if (st_gdata != NULL) {
917 /* Free ST Tx Qs and skbs */
918 skb_queue_purge(&st_gdata->txq);
919 skb_queue_purge(&st_gdata->tx_waitq);
920 kfree_skb(st_gdata->rx_skb);
921 kfree_skb(st_gdata->tx_skb);
922 /* TTY ldisc cleanup */
923 err = tty_unregister_ldisc(N_TI_WL);
925 pr_err("unable to un-register ldisc %ld", err);
926 /* free the global data pointer */