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
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>
19 extern void st_kim_recv(void *, const unsigned char *, long);
20 void st_int_recv(void *, const unsigned char *, long);
22 * function pointer pointing to either,
23 * st_kim_recv during registration to receive fw download responses
24 * st_int_recv after registration to receive proto stack responses
26 static void (*st_recv) (void *, const unsigned char *, long);
28 /********************************************************************/
29 static void add_channel_to_table(struct st_data_s *st_gdata,
30 struct st_proto_s *new_proto)
32 pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
33 /* list now has the channel id as index itself */
34 st_gdata->list[new_proto->chnl_id] = new_proto;
35 st_gdata->is_registered[new_proto->chnl_id] = true;
38 static void remove_channel_from_table(struct st_data_s *st_gdata,
39 struct st_proto_s *proto)
41 pr_info("%s: id %d\n", __func__, proto->chnl_id);
42 /* st_gdata->list[proto->chnl_id] = NULL; */
43 st_gdata->is_registered[proto->chnl_id] = false;
47 * called from KIM during firmware download.
49 * This is a wrapper function to tty->ops->write_room.
50 * It returns number of free space available in
53 int st_get_uart_wr_room(struct st_data_s *st_gdata)
55 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
56 pr_err("tty unavailable to perform write");
60 return tty_write_room(st_gdata->tty);
64 * can be called in from
65 * -- KIM (during fw download)
66 * -- ST Core (during st_write)
68 * This is the internal write function - a wrapper
71 int st_int_write(struct st_data_s *st_gdata,
72 const unsigned char *data, int count)
74 struct tty_struct *tty;
75 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
76 pr_err("tty unavailable to perform write");
81 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
82 16, 1, data, count, 0);
84 return tty->ops->write(tty, data, count);
89 * push the skb received to relevant
92 static void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
94 pr_debug(" %s(prot:%d) ", __func__, chnl_id);
97 (st_gdata == NULL || st_gdata->rx_skb == NULL
98 || st_gdata->is_registered[chnl_id] == false)) {
99 pr_err("chnl_id %d not registered, no data to send?",
101 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[chnl_id]->recv != NULL)) {
112 (st_gdata->list[chnl_id]->recv
113 (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
115 pr_err(" proto stack %d's ->recv failed", chnl_id);
116 kfree_skb(st_gdata->rx_skb);
120 pr_err(" proto stack %d's ->recv null", chnl_id);
121 kfree_skb(st_gdata->rx_skb);
127 * st_reg_complete - to call registration complete callbacks
128 * of all protocol stack drivers
129 * This function is being called with spin lock held, protocol drivers are
130 * only expected to complete their waits and do nothing more than that.
132 static void st_reg_complete(struct st_data_s *st_gdata, int err)
135 pr_info(" %s ", __func__);
136 for (i = 0; i < ST_MAX_CHANNELS; i++) {
137 if (likely(st_gdata != NULL &&
138 st_gdata->is_registered[i] == true &&
139 st_gdata->list[i]->reg_complete_cb != NULL)) {
140 st_gdata->list[i]->reg_complete_cb
141 (st_gdata->list[i]->priv_data, err);
142 pr_info("protocol %d's cb sent %d\n", i, err);
143 if (err) { /* cleanup registered protocol */
144 st_gdata->is_registered[i] = false;
145 if (st_gdata->protos_registered)
146 st_gdata->protos_registered--;
152 static inline int st_check_data_len(struct st_data_s *st_gdata,
153 unsigned char chnl_id, int len)
155 int room = skb_tailroom(st_gdata->rx_skb);
157 pr_debug("len %d room %d", len, room);
161 * Received packet has only packet header and
162 * has zero length payload. So, ask ST CORE to
163 * forward the packet to protocol driver (BT/FM/GPS)
165 st_send_frame(chnl_id, st_gdata);
167 } else if (len > room) {
169 * Received packet's payload length is larger.
170 * We can't accommodate it in created skb.
172 pr_err("Data length is too large len %d room %d", len,
174 kfree_skb(st_gdata->rx_skb);
177 * Packet header has non-zero payload length and
178 * we have enough space in created skb. Lets read
180 st_gdata->rx_state = ST_W4_DATA;
181 st_gdata->rx_count = len;
185 /* Change ST state to continue to process next packet */
186 st_gdata->rx_state = ST_W4_PACKET_TYPE;
187 st_gdata->rx_skb = NULL;
188 st_gdata->rx_count = 0;
189 st_gdata->rx_chnl = 0;
195 * st_wakeup_ack - internal function for action when wake-up ack
198 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
201 struct sk_buff *waiting_skb;
202 unsigned long flags = 0;
204 spin_lock_irqsave(&st_gdata->lock, flags);
206 * de-Q from waitQ and Q in txQ now that the
209 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
210 skb_queue_tail(&st_gdata->txq, waiting_skb);
212 /* state forwarded to ST LL */
213 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
214 spin_unlock_irqrestore(&st_gdata->lock, flags);
216 /* wake up to send the recently copied skbs from waitQ */
217 st_tx_wakeup(st_gdata);
221 * st_int_recv - ST's internal receive function.
222 * Decodes received RAW data and forwards to corresponding
223 * client drivers (Bluetooth,FM,GPS..etc).
224 * This can receive various types of packets,
225 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
226 * CH-8 packets from FM, CH-9 packets from GPS cores.
228 void st_int_recv(void *disc_data,
229 const unsigned char *data, long count)
232 struct st_proto_s *proto;
233 unsigned short payload_len = 0;
235 unsigned char type = 0;
237 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
241 /* tty_receive sent null ? */
242 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
243 pr_err(" received null from TTY ");
247 pr_debug("count %ld rx_state %ld"
248 "rx_count %ld", count, st_gdata->rx_state,
251 spin_lock_irqsave(&st_gdata->lock, flags);
252 /* Decode received bytes here */
254 if (st_gdata->rx_count) {
255 len = min_t(unsigned int, st_gdata->rx_count, count);
256 skb_put_data(st_gdata->rx_skb, ptr, len);
257 st_gdata->rx_count -= len;
261 if (st_gdata->rx_count)
264 /* Check ST RX state machine , where are we? */
265 switch (st_gdata->rx_state) {
266 /* Waiting for complete packet ? */
268 pr_debug("Complete pkt received");
270 * Ask ST CORE to forward
271 * the packet to protocol driver
273 st_send_frame(st_gdata->rx_chnl, st_gdata);
275 st_gdata->rx_state = ST_W4_PACKET_TYPE;
276 st_gdata->rx_skb = NULL;
278 /* parse the header to know details */
280 proto = st_gdata->list[st_gdata->rx_chnl];
282 &st_gdata->rx_skb->data
283 [proto->offset_len_in_hdr];
284 pr_debug("plen pointing to %x\n", *plen);
285 if (proto->len_size == 1) /* 1 byte len field */
286 payload_len = *(unsigned char *)plen;
287 else if (proto->len_size == 2)
289 __le16_to_cpu(*(unsigned short *)plen);
291 pr_info("%s: invalid length "
293 __func__, proto->chnl_id);
294 st_check_data_len(st_gdata, proto->chnl_id,
296 pr_debug("off %d, pay len %d\n",
297 proto->offset_len_in_hdr, payload_len);
299 } /* end of switch rx_state */
302 /* end of if rx_count */
305 * Check first byte of packet and identify module
312 pr_debug("PM packet");
314 * this takes appropriate action based on
315 * sleep state received --
317 st_ll_sleep_state(st_gdata, *ptr);
319 * if WAKEUP_IND collides copy from waitq to txq
320 * and assume chip awake
322 spin_unlock_irqrestore(&st_gdata->lock, flags);
323 if (st_ll_getstate(st_gdata) == ST_LL_AWAKE)
324 st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK);
325 spin_lock_irqsave(&st_gdata->lock, flags);
331 pr_debug("PM packet");
333 spin_unlock_irqrestore(&st_gdata->lock, flags);
334 /* wake up ack received */
335 st_wakeup_ack(st_gdata, *ptr);
336 spin_lock_irqsave(&st_gdata->lock, flags);
346 * Default case means non-HCILL packets,
347 * possibilities are packets for:
348 * (a) valid protocol - Supported Protocols within
349 * the ST_MAX_CHANNELS.
350 * (b) registered protocol - Checked by
351 * "st_gdata->list[type] == NULL)" are supported
353 * Rules out any invalid protocol and
354 * unregistered protocols with channel ID < 16.
357 if ((type >= ST_MAX_CHANNELS) ||
358 (st_gdata->list[type] == NULL)) {
359 pr_err("chip/interface misbehavior: "
360 "dropping frame starting "
361 "with 0x%02x\n", type);
365 st_gdata->rx_skb = alloc_skb(
366 st_gdata->list[type]->max_frame_size,
368 if (st_gdata->rx_skb == NULL) {
369 pr_err("out of memory: dropping\n");
373 skb_reserve(st_gdata->rx_skb,
374 st_gdata->list[type]->reserve);
375 /* next 2 required for BT only */
376 st_gdata->rx_skb->cb[0] = type; /*pkt_type*/
377 st_gdata->rx_skb->cb[1] = 0; /*incoming*/
378 st_gdata->rx_chnl = *ptr;
379 st_gdata->rx_state = ST_W4_HEADER;
380 st_gdata->rx_count = st_gdata->list[type]->hdr_len;
381 pr_debug("rx_count %ld\n", st_gdata->rx_count);
387 spin_unlock_irqrestore(&st_gdata->lock, flags);
388 pr_debug("done %s", __func__);
393 * st_int_dequeue - internal de-Q function.
394 * If the previous data set was not written
395 * completely, return that skb which has the pending data.
396 * In normal cases, return top of txq.
398 static struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
400 struct sk_buff *returning_skb;
402 pr_debug("%s", __func__);
403 if (st_gdata->tx_skb != NULL) {
404 returning_skb = st_gdata->tx_skb;
405 st_gdata->tx_skb = NULL;
406 return returning_skb;
408 return skb_dequeue(&st_gdata->txq);
412 * st_int_enqueue - internal Q-ing function.
413 * Will either Q the skb to txq or the tx_waitq
414 * depending on the ST LL state.
415 * If the chip is asleep, then Q it onto waitq and
417 * txq and waitq needs protection since the other contexts
418 * may be sending data, waking up chip.
420 static void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
422 unsigned long flags = 0;
424 pr_debug("%s", __func__);
425 spin_lock_irqsave(&st_gdata->lock, flags);
427 switch (st_ll_getstate(st_gdata)) {
429 pr_debug("ST LL is AWAKE, sending normally");
430 skb_queue_tail(&st_gdata->txq, skb);
432 case ST_LL_ASLEEP_TO_AWAKE:
433 skb_queue_tail(&st_gdata->tx_waitq, skb);
435 case ST_LL_AWAKE_TO_ASLEEP:
436 pr_err("ST LL is illegal state(%ld),"
437 "purging received skb.", st_ll_getstate(st_gdata));
441 skb_queue_tail(&st_gdata->tx_waitq, skb);
442 st_ll_wakeup(st_gdata);
445 pr_err("ST LL is illegal state(%ld),"
446 "purging received skb.", st_ll_getstate(st_gdata));
451 spin_unlock_irqrestore(&st_gdata->lock, flags);
452 pr_debug("done %s", __func__);
457 * internal wakeup function
459 * - TTY layer when write's finished
460 * - st_write (in context of the protocol stack)
462 static void work_fn_write_wakeup(struct work_struct *work)
464 struct st_data_s *st_gdata = container_of(work, struct st_data_s,
467 st_tx_wakeup((void *)st_gdata);
469 void st_tx_wakeup(struct st_data_s *st_data)
472 unsigned long flags; /* for irq save flags */
473 pr_debug("%s", __func__);
474 /* check for sending & set flag sending here */
475 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
476 pr_debug("ST already sending");
478 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
480 /* TX_WAKEUP will be checked in another
484 do { /* come back if st_tx_wakeup is set */
485 /* woke-up to write */
486 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
487 while ((skb = st_int_dequeue(st_data))) {
489 spin_lock_irqsave(&st_data->lock, flags);
490 /* enable wake-up from TTY */
491 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
492 len = st_int_write(st_data, skb->data, skb->len);
494 /* if skb->len = len as expected, skb->len=0 */
496 /* would be the next skb to be sent */
497 st_data->tx_skb = skb;
498 spin_unlock_irqrestore(&st_data->lock, flags);
502 spin_unlock_irqrestore(&st_data->lock, flags);
504 /* if wake-up is set in another context- restart sending */
505 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
507 /* clear flag sending */
508 clear_bit(ST_TX_SENDING, &st_data->tx_state);
511 /********************************************************************/
512 /* functions called from ST KIM
514 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
516 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
517 st_gdata->protos_registered,
518 st_gdata->is_registered[0x04] == true ? 'R' : 'U',
519 st_gdata->is_registered[0x08] == true ? 'R' : 'U',
520 st_gdata->is_registered[0x09] == true ? 'R' : 'U');
523 /********************************************************************/
525 * functions called from protocol stack drivers
528 long st_register(struct st_proto_s *new_proto)
530 struct st_data_s *st_gdata;
532 unsigned long flags = 0;
534 st_kim_ref(&st_gdata, 0);
535 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
536 || new_proto->reg_complete_cb == NULL) {
537 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
541 if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
542 pr_err("chnl_id %d not supported", new_proto->chnl_id);
543 return -EPROTONOSUPPORT;
546 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
547 pr_err("chnl_id %d already registered", new_proto->chnl_id);
551 /* can be from process context only */
552 spin_lock_irqsave(&st_gdata->lock, flags);
554 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
555 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
556 /* fw download in progress */
558 add_channel_to_table(st_gdata, new_proto);
559 st_gdata->protos_registered++;
560 new_proto->write = st_write;
562 set_bit(ST_REG_PENDING, &st_gdata->st_state);
563 spin_unlock_irqrestore(&st_gdata->lock, flags);
565 } else if (st_gdata->protos_registered == ST_EMPTY) {
566 pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
567 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
568 st_recv = st_kim_recv;
570 /* enable the ST LL - to set default chip state */
571 st_ll_enable(st_gdata);
573 /* release lock previously held - re-locked below */
574 spin_unlock_irqrestore(&st_gdata->lock, flags);
577 * this may take a while to complete
578 * since it involves BT fw download
580 err = st_kim_start(st_gdata->kim_data);
582 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
583 if ((st_gdata->protos_registered != ST_EMPTY) &&
584 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
585 pr_err(" KIM failure complete callback ");
586 spin_lock_irqsave(&st_gdata->lock, flags);
587 st_reg_complete(st_gdata, err);
588 spin_unlock_irqrestore(&st_gdata->lock, flags);
589 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
594 spin_lock_irqsave(&st_gdata->lock, flags);
596 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
597 st_recv = st_int_recv;
600 * this is where all pending registration
601 * are signalled to be complete by calling callback functions
603 if ((st_gdata->protos_registered != ST_EMPTY) &&
604 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
605 pr_debug(" call reg complete callback ");
606 st_reg_complete(st_gdata, 0);
608 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
611 * check for already registered once more,
612 * since the above check is old
614 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
615 pr_err(" proto %d already registered ",
617 spin_unlock_irqrestore(&st_gdata->lock, flags);
621 add_channel_to_table(st_gdata, new_proto);
622 st_gdata->protos_registered++;
623 new_proto->write = st_write;
624 spin_unlock_irqrestore(&st_gdata->lock, flags);
627 /* if fw is already downloaded & new stack registers protocol */
629 add_channel_to_table(st_gdata, new_proto);
630 st_gdata->protos_registered++;
631 new_proto->write = st_write;
633 /* lock already held before entering else */
634 spin_unlock_irqrestore(&st_gdata->lock, flags);
638 EXPORT_SYMBOL_GPL(st_register);
641 * to unregister a protocol -
642 * to be called from protocol stack driver
644 long st_unregister(struct st_proto_s *proto)
647 unsigned long flags = 0;
648 struct st_data_s *st_gdata;
650 pr_debug("%s: %d ", __func__, proto->chnl_id);
652 st_kim_ref(&st_gdata, 0);
653 if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
654 pr_err(" chnl_id %d not supported", proto->chnl_id);
655 return -EPROTONOSUPPORT;
658 spin_lock_irqsave(&st_gdata->lock, flags);
660 if (st_gdata->is_registered[proto->chnl_id] == false) {
661 pr_err(" chnl_id %d not registered", proto->chnl_id);
662 spin_unlock_irqrestore(&st_gdata->lock, flags);
663 return -EPROTONOSUPPORT;
666 if (st_gdata->protos_registered)
667 st_gdata->protos_registered--;
669 remove_channel_from_table(st_gdata, proto);
670 spin_unlock_irqrestore(&st_gdata->lock, flags);
672 if ((st_gdata->protos_registered == ST_EMPTY) &&
673 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
674 pr_info(" all chnl_ids unregistered ");
676 /* stop traffic on tty */
678 tty_ldisc_flush(st_gdata->tty);
679 stop_tty(st_gdata->tty);
682 /* all chnl_ids now unregistered */
683 st_kim_stop(st_gdata->kim_data);
685 st_ll_disable(st_gdata);
691 * called in protocol stack drivers
692 * via the write function pointer
694 long st_write(struct sk_buff *skb)
696 struct st_data_s *st_gdata;
699 st_kim_ref(&st_gdata, 0);
700 if (unlikely(skb == NULL || st_gdata == NULL
701 || st_gdata->tty == NULL)) {
702 pr_err("data/tty unavailable to perform write");
706 pr_debug("%d to be written", skb->len);
709 /* st_ll to decide where to enqueue the skb */
710 st_int_enqueue(st_gdata, skb);
712 st_tx_wakeup(st_gdata);
714 /* return number of bytes written */
718 /* for protocols making use of shared transport */
719 EXPORT_SYMBOL_GPL(st_unregister);
721 /********************************************************************/
723 * functions called from TTY layer
725 static int st_tty_open(struct tty_struct *tty)
727 struct st_data_s *st_gdata;
728 pr_info("%s ", __func__);
730 st_kim_ref(&st_gdata, 0);
732 tty->disc_data = st_gdata;
734 /* don't do an wakeup for now */
735 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
737 /* mem already allocated
739 tty->receive_room = 65536;
740 /* Flush any pending characters in the driver and discipline. */
741 tty_ldisc_flush(tty);
742 tty_driver_flush_buffer(tty);
744 * signal to UIM via KIM that -
745 * installation of N_TI_WL ldisc is complete
747 st_kim_complete(st_gdata->kim_data);
748 pr_debug("done %s", __func__);
753 static void st_tty_close(struct tty_struct *tty)
757 struct st_data_s *st_gdata = tty->disc_data;
759 pr_info("%s ", __func__);
763 * if a protocol has been registered & line discipline
764 * un-installed for some reason - what should be done ?
766 spin_lock_irqsave(&st_gdata->lock, flags);
767 for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
768 if (st_gdata->is_registered[i] == true)
769 pr_err("%d not un-registered", i);
770 st_gdata->list[i] = NULL;
771 st_gdata->is_registered[i] = false;
773 st_gdata->protos_registered = 0;
774 spin_unlock_irqrestore(&st_gdata->lock, flags);
776 * signal to UIM via KIM that -
777 * N_TI_WL ldisc is un-installed
779 st_kim_complete(st_gdata->kim_data);
780 st_gdata->tty = NULL;
781 /* Flush any pending characters in the driver and discipline. */
782 tty_ldisc_flush(tty);
783 tty_driver_flush_buffer(tty);
785 spin_lock_irqsave(&st_gdata->lock, flags);
786 /* empty out txq and tx_waitq */
787 skb_queue_purge(&st_gdata->txq);
788 skb_queue_purge(&st_gdata->tx_waitq);
789 /* reset the TTY Rx states of ST */
790 st_gdata->rx_count = 0;
791 st_gdata->rx_state = ST_W4_PACKET_TYPE;
792 kfree_skb(st_gdata->rx_skb);
793 st_gdata->rx_skb = NULL;
794 spin_unlock_irqrestore(&st_gdata->lock, flags);
796 pr_debug("%s: done ", __func__);
799 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
800 const char *tty_flags, int count)
803 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
804 16, 1, data, count, 0);
808 * if fw download is in progress then route incoming data
809 * to KIM for validation
811 st_recv(tty->disc_data, data, count);
812 pr_debug("done %s", __func__);
816 * wake-up function called in from the TTY layer
817 * inside the internal wakeup function will be called
819 static void st_tty_wakeup(struct tty_struct *tty)
821 struct st_data_s *st_gdata = tty->disc_data;
822 pr_debug("%s ", __func__);
823 /* don't do an wakeup for now */
824 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
827 * schedule the internal wakeup instead of calling directly to
828 * avoid lockup (port->lock needed in tty->ops->write is
831 schedule_work(&st_gdata->work_write_wakeup);
834 static void st_tty_flush_buffer(struct tty_struct *tty)
836 struct st_data_s *st_gdata = tty->disc_data;
837 pr_debug("%s ", __func__);
839 kfree_skb(st_gdata->tx_skb);
840 st_gdata->tx_skb = NULL;
842 tty_driver_flush_buffer(tty);
846 static struct tty_ldisc_ops st_ldisc_ops = {
850 .close = st_tty_close,
851 .receive_buf = st_tty_receive,
852 .write_wakeup = st_tty_wakeup,
853 .flush_buffer = st_tty_flush_buffer,
857 /********************************************************************/
858 int st_core_init(struct st_data_s **core_data)
860 struct st_data_s *st_gdata;
863 err = tty_register_ldisc(&st_ldisc_ops);
865 pr_err("error registering %d line discipline %ld",
869 pr_debug("registered n_shared line discipline");
871 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
873 pr_err("memory allocation failed");
875 goto err_unreg_ldisc;
878 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
879 * will be pushed in this queue for actual transmission.
881 skb_queue_head_init(&st_gdata->txq);
882 skb_queue_head_init(&st_gdata->tx_waitq);
884 /* Locking used in st_int_enqueue() to avoid multiple execution */
885 spin_lock_init(&st_gdata->lock);
887 err = st_ll_init(st_gdata);
889 pr_err("error during st_ll initialization(%ld)", err);
893 INIT_WORK(&st_gdata->work_write_wakeup, work_fn_write_wakeup);
895 *core_data = st_gdata;
900 tty_unregister_ldisc(&st_ldisc_ops);
904 void st_core_exit(struct st_data_s *st_gdata)
907 /* internal module cleanup */
908 err = st_ll_deinit(st_gdata);
910 pr_err("error during deinit of ST LL %ld", err);
912 if (st_gdata != NULL) {
913 /* Free ST Tx Qs and skbs */
914 skb_queue_purge(&st_gdata->txq);
915 skb_queue_purge(&st_gdata->tx_waitq);
916 kfree_skb(st_gdata->rx_skb);
917 kfree_skb(st_gdata->tx_skb);
918 /* TTY ldisc cleanup */
919 tty_unregister_ldisc(&st_ldisc_ops);
920 /* free the global data pointer */