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>
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 struct tty_struct
*tty
;
56 if (unlikely(st_gdata
== NULL
|| st_gdata
->tty
== NULL
)) {
57 pr_err("tty unavailable to perform write");
61 return tty
->ops
->write_room(tty
);
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 static void st_send_frame(unsigned char chnl_id
, struct st_data_s
*st_gdata
)
95 pr_debug(" %s(prot:%d) ", __func__
, chnl_id
);
98 (st_gdata
== NULL
|| st_gdata
->rx_skb
== NULL
99 || st_gdata
->is_registered
[chnl_id
] == false)) {
100 pr_err("chnl_id %d not registered, no data to send?",
102 kfree_skb(st_gdata
->rx_skb
);
107 * this shouldn't take long
108 * - should be just skb_queue_tail for the
109 * protocol stack driver
111 if (likely(st_gdata
->list
[chnl_id
]->recv
!= NULL
)) {
113 (st_gdata
->list
[chnl_id
]->recv
114 (st_gdata
->list
[chnl_id
]->priv_data
, st_gdata
->rx_skb
)
116 pr_err(" proto stack %d's ->recv failed", chnl_id
);
117 kfree_skb(st_gdata
->rx_skb
);
121 pr_err(" proto stack %d's ->recv null", chnl_id
);
122 kfree_skb(st_gdata
->rx_skb
);
128 * st_reg_complete - to call registration complete callbacks
129 * of all protocol stack drivers
130 * This function is being called with spin lock held, protocol drivers are
131 * only expected to complete their waits and do nothing more than that.
133 static void st_reg_complete(struct st_data_s
*st_gdata
, int err
)
136 pr_info(" %s ", __func__
);
137 for (i
= 0; i
< ST_MAX_CHANNELS
; i
++) {
138 if (likely(st_gdata
!= NULL
&&
139 st_gdata
->is_registered
[i
] == true &&
140 st_gdata
->list
[i
]->reg_complete_cb
!= NULL
)) {
141 st_gdata
->list
[i
]->reg_complete_cb
142 (st_gdata
->list
[i
]->priv_data
, err
);
143 pr_info("protocol %d's cb sent %d\n", i
, err
);
144 if (err
) { /* cleanup registered protocol */
145 st_gdata
->is_registered
[i
] = false;
146 if (st_gdata
->protos_registered
)
147 st_gdata
->protos_registered
--;
153 static inline int st_check_data_len(struct st_data_s
*st_gdata
,
154 unsigned char chnl_id
, int len
)
156 int room
= skb_tailroom(st_gdata
->rx_skb
);
158 pr_debug("len %d room %d", len
, room
);
162 * Received packet has only packet header and
163 * has zero length payload. So, ask ST CORE to
164 * forward the packet to protocol driver (BT/FM/GPS)
166 st_send_frame(chnl_id
, st_gdata
);
168 } else if (len
> room
) {
170 * Received packet's payload length is larger.
171 * We can't accommodate it in created skb.
173 pr_err("Data length is too large len %d room %d", len
,
175 kfree_skb(st_gdata
->rx_skb
);
178 * Packet header has non-zero payload length and
179 * we have enough space in created skb. Lets read
181 st_gdata
->rx_state
= ST_W4_DATA
;
182 st_gdata
->rx_count
= len
;
186 /* Change ST state to continue to process next packet */
187 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
188 st_gdata
->rx_skb
= NULL
;
189 st_gdata
->rx_count
= 0;
190 st_gdata
->rx_chnl
= 0;
196 * st_wakeup_ack - internal function for action when wake-up ack
199 static inline void st_wakeup_ack(struct st_data_s
*st_gdata
,
202 struct sk_buff
*waiting_skb
;
203 unsigned long flags
= 0;
205 spin_lock_irqsave(&st_gdata
->lock
, flags
);
207 * de-Q from waitQ and Q in txQ now that the
210 while ((waiting_skb
= skb_dequeue(&st_gdata
->tx_waitq
)))
211 skb_queue_tail(&st_gdata
->txq
, waiting_skb
);
213 /* state forwarded to ST LL */
214 st_ll_sleep_state(st_gdata
, (unsigned long)cmd
);
215 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
217 /* wake up to send the recently copied skbs from waitQ */
218 st_tx_wakeup(st_gdata
);
222 * st_int_recv - ST's internal receive function.
223 * Decodes received RAW data and forwards to corresponding
224 * client drivers (Bluetooth,FM,GPS..etc).
225 * This can receive various types of packets,
226 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
227 * CH-8 packets from FM, CH-9 packets from GPS cores.
229 void st_int_recv(void *disc_data
,
230 const unsigned char *data
, long count
)
233 struct st_proto_s
*proto
;
234 unsigned short payload_len
= 0;
236 unsigned char type
= 0;
238 struct st_data_s
*st_gdata
= (struct st_data_s
*)disc_data
;
242 /* tty_receive sent null ? */
243 if (unlikely(ptr
== NULL
) || (st_gdata
== NULL
)) {
244 pr_err(" received null from TTY ");
248 pr_debug("count %ld rx_state %ld"
249 "rx_count %ld", count
, st_gdata
->rx_state
,
252 spin_lock_irqsave(&st_gdata
->lock
, flags
);
253 /* Decode received bytes here */
255 if (st_gdata
->rx_count
) {
256 len
= min_t(unsigned int, st_gdata
->rx_count
, count
);
257 skb_put_data(st_gdata
->rx_skb
, ptr
, len
);
258 st_gdata
->rx_count
-= len
;
262 if (st_gdata
->rx_count
)
265 /* Check ST RX state machine , where are we? */
266 switch (st_gdata
->rx_state
) {
267 /* Waiting for complete packet ? */
269 pr_debug("Complete pkt received");
271 * Ask ST CORE to forward
272 * the packet to protocol driver
274 st_send_frame(st_gdata
->rx_chnl
, st_gdata
);
276 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
277 st_gdata
->rx_skb
= NULL
;
279 /* parse the header to know details */
281 proto
= st_gdata
->list
[st_gdata
->rx_chnl
];
283 &st_gdata
->rx_skb
->data
284 [proto
->offset_len_in_hdr
];
285 pr_debug("plen pointing to %x\n", *plen
);
286 if (proto
->len_size
== 1) /* 1 byte len field */
287 payload_len
= *(unsigned char *)plen
;
288 else if (proto
->len_size
== 2)
290 __le16_to_cpu(*(unsigned short *)plen
);
292 pr_info("%s: invalid length "
294 __func__
, proto
->chnl_id
);
295 st_check_data_len(st_gdata
, proto
->chnl_id
,
297 pr_debug("off %d, pay len %d\n",
298 proto
->offset_len_in_hdr
, payload_len
);
300 } /* end of switch rx_state */
303 /* end of if rx_count */
306 * Check first byte of packet and identify module
313 pr_debug("PM packet");
315 * this takes appropriate action based on
316 * sleep state received --
318 st_ll_sleep_state(st_gdata
, *ptr
);
320 * if WAKEUP_IND collides copy from waitq to txq
321 * and assume chip awake
323 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
324 if (st_ll_getstate(st_gdata
) == ST_LL_AWAKE
)
325 st_wakeup_ack(st_gdata
, LL_WAKE_UP_ACK
);
326 spin_lock_irqsave(&st_gdata
->lock
, flags
);
332 pr_debug("PM packet");
334 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
335 /* wake up ack received */
336 st_wakeup_ack(st_gdata
, *ptr
);
337 spin_lock_irqsave(&st_gdata
->lock
, flags
);
347 * Default case means non-HCILL packets,
348 * possibilities are packets for:
349 * (a) valid protocol - Supported Protocols within
350 * the ST_MAX_CHANNELS.
351 * (b) registered protocol - Checked by
352 * "st_gdata->list[type] == NULL)" are supported
354 * Rules out any invalid protocol and
355 * unregistered protocols with channel ID < 16.
358 if ((type
>= ST_MAX_CHANNELS
) ||
359 (st_gdata
->list
[type
] == NULL
)) {
360 pr_err("chip/interface misbehavior: "
361 "dropping frame starting "
362 "with 0x%02x\n", type
);
366 st_gdata
->rx_skb
= alloc_skb(
367 st_gdata
->list
[type
]->max_frame_size
,
369 if (st_gdata
->rx_skb
== NULL
) {
370 pr_err("out of memory: dropping\n");
374 skb_reserve(st_gdata
->rx_skb
,
375 st_gdata
->list
[type
]->reserve
);
376 /* next 2 required for BT only */
377 st_gdata
->rx_skb
->cb
[0] = type
; /*pkt_type*/
378 st_gdata
->rx_skb
->cb
[1] = 0; /*incoming*/
379 st_gdata
->rx_chnl
= *ptr
;
380 st_gdata
->rx_state
= ST_W4_HEADER
;
381 st_gdata
->rx_count
= st_gdata
->list
[type
]->hdr_len
;
382 pr_debug("rx_count %ld\n", st_gdata
->rx_count
);
388 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
389 pr_debug("done %s", __func__
);
394 * st_int_dequeue - internal de-Q function.
395 * If the previous data set was not written
396 * completely, return that skb which has the pending data.
397 * In normal cases, return top of txq.
399 static struct sk_buff
*st_int_dequeue(struct st_data_s
*st_gdata
)
401 struct sk_buff
*returning_skb
;
403 pr_debug("%s", __func__
);
404 if (st_gdata
->tx_skb
!= NULL
) {
405 returning_skb
= st_gdata
->tx_skb
;
406 st_gdata
->tx_skb
= NULL
;
407 return returning_skb
;
409 return skb_dequeue(&st_gdata
->txq
);
413 * st_int_enqueue - internal Q-ing function.
414 * Will either Q the skb to txq or the tx_waitq
415 * depending on the ST LL state.
416 * If the chip is asleep, then Q it onto waitq and
418 * txq and waitq needs protection since the other contexts
419 * may be sending data, waking up chip.
421 static void st_int_enqueue(struct st_data_s
*st_gdata
, struct sk_buff
*skb
)
423 unsigned long flags
= 0;
425 pr_debug("%s", __func__
);
426 spin_lock_irqsave(&st_gdata
->lock
, flags
);
428 switch (st_ll_getstate(st_gdata
)) {
430 pr_debug("ST LL is AWAKE, sending normally");
431 skb_queue_tail(&st_gdata
->txq
, skb
);
433 case ST_LL_ASLEEP_TO_AWAKE
:
434 skb_queue_tail(&st_gdata
->tx_waitq
, skb
);
436 case ST_LL_AWAKE_TO_ASLEEP
:
437 pr_err("ST LL is illegal state(%ld),"
438 "purging received skb.", st_ll_getstate(st_gdata
));
442 skb_queue_tail(&st_gdata
->tx_waitq
, skb
);
443 st_ll_wakeup(st_gdata
);
446 pr_err("ST LL is illegal state(%ld),"
447 "purging received skb.", st_ll_getstate(st_gdata
));
452 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
453 pr_debug("done %s", __func__
);
458 * internal wakeup function
460 * - TTY layer when write's finished
461 * - st_write (in context of the protocol stack)
463 static void work_fn_write_wakeup(struct work_struct
*work
)
465 struct st_data_s
*st_gdata
= container_of(work
, struct st_data_s
,
468 st_tx_wakeup((void *)st_gdata
);
470 void st_tx_wakeup(struct st_data_s
*st_data
)
473 unsigned long flags
; /* for irq save flags */
474 pr_debug("%s", __func__
);
475 /* check for sending & set flag sending here */
476 if (test_and_set_bit(ST_TX_SENDING
, &st_data
->tx_state
)) {
477 pr_debug("ST already sending");
479 set_bit(ST_TX_WAKEUP
, &st_data
->tx_state
);
481 /* TX_WAKEUP will be checked in another
485 do { /* come back if st_tx_wakeup is set */
486 /* woke-up to write */
487 clear_bit(ST_TX_WAKEUP
, &st_data
->tx_state
);
488 while ((skb
= st_int_dequeue(st_data
))) {
490 spin_lock_irqsave(&st_data
->lock
, flags
);
491 /* enable wake-up from TTY */
492 set_bit(TTY_DO_WRITE_WAKEUP
, &st_data
->tty
->flags
);
493 len
= st_int_write(st_data
, skb
->data
, skb
->len
);
495 /* if skb->len = len as expected, skb->len=0 */
497 /* would be the next skb to be sent */
498 st_data
->tx_skb
= skb
;
499 spin_unlock_irqrestore(&st_data
->lock
, flags
);
503 spin_unlock_irqrestore(&st_data
->lock
, flags
);
505 /* if wake-up is set in another context- restart sending */
506 } while (test_bit(ST_TX_WAKEUP
, &st_data
->tx_state
));
508 /* clear flag sending */
509 clear_bit(ST_TX_SENDING
, &st_data
->tx_state
);
512 /********************************************************************/
513 /* functions called from ST KIM
515 void kim_st_list_protocols(struct st_data_s
*st_gdata
, void *buf
)
517 seq_printf(buf
, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
518 st_gdata
->protos_registered
,
519 st_gdata
->is_registered
[0x04] == true ? 'R' : 'U',
520 st_gdata
->is_registered
[0x08] == true ? 'R' : 'U',
521 st_gdata
->is_registered
[0x09] == true ? 'R' : 'U');
524 /********************************************************************/
526 * functions called from protocol stack drivers
529 long st_register(struct st_proto_s
*new_proto
)
531 struct st_data_s
*st_gdata
;
533 unsigned long flags
= 0;
535 st_kim_ref(&st_gdata
, 0);
536 if (st_gdata
== NULL
|| new_proto
== NULL
|| new_proto
->recv
== NULL
537 || new_proto
->reg_complete_cb
== NULL
) {
538 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
542 if (new_proto
->chnl_id
>= ST_MAX_CHANNELS
) {
543 pr_err("chnl_id %d not supported", new_proto
->chnl_id
);
544 return -EPROTONOSUPPORT
;
547 if (st_gdata
->is_registered
[new_proto
->chnl_id
] == true) {
548 pr_err("chnl_id %d already registered", new_proto
->chnl_id
);
552 /* can be from process context only */
553 spin_lock_irqsave(&st_gdata
->lock
, flags
);
555 if (test_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
)) {
556 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto
->chnl_id
);
557 /* fw download in progress */
559 add_channel_to_table(st_gdata
, new_proto
);
560 st_gdata
->protos_registered
++;
561 new_proto
->write
= st_write
;
563 set_bit(ST_REG_PENDING
, &st_gdata
->st_state
);
564 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
566 } else if (st_gdata
->protos_registered
== ST_EMPTY
) {
567 pr_info(" chnl_id list empty :%d ", new_proto
->chnl_id
);
568 set_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
569 st_recv
= st_kim_recv
;
571 /* enable the ST LL - to set default chip state */
572 st_ll_enable(st_gdata
);
574 /* release lock previously held - re-locked below */
575 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
578 * this may take a while to complete
579 * since it involves BT fw download
581 err
= st_kim_start(st_gdata
->kim_data
);
583 clear_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
584 if ((st_gdata
->protos_registered
!= ST_EMPTY
) &&
585 (test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
586 pr_err(" KIM failure complete callback ");
587 spin_lock_irqsave(&st_gdata
->lock
, flags
);
588 st_reg_complete(st_gdata
, err
);
589 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
590 clear_bit(ST_REG_PENDING
, &st_gdata
->st_state
);
595 spin_lock_irqsave(&st_gdata
->lock
, flags
);
597 clear_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
598 st_recv
= st_int_recv
;
601 * this is where all pending registration
602 * are signalled to be complete by calling callback functions
604 if ((st_gdata
->protos_registered
!= ST_EMPTY
) &&
605 (test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
606 pr_debug(" call reg complete callback ");
607 st_reg_complete(st_gdata
, 0);
609 clear_bit(ST_REG_PENDING
, &st_gdata
->st_state
);
612 * check for already registered once more,
613 * since the above check is old
615 if (st_gdata
->is_registered
[new_proto
->chnl_id
] == true) {
616 pr_err(" proto %d already registered ",
618 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
622 add_channel_to_table(st_gdata
, new_proto
);
623 st_gdata
->protos_registered
++;
624 new_proto
->write
= st_write
;
625 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
628 /* if fw is already downloaded & new stack registers protocol */
630 add_channel_to_table(st_gdata
, new_proto
);
631 st_gdata
->protos_registered
++;
632 new_proto
->write
= st_write
;
634 /* lock already held before entering else */
635 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
639 EXPORT_SYMBOL_GPL(st_register
);
642 * to unregister a protocol -
643 * to be called from protocol stack driver
645 long st_unregister(struct st_proto_s
*proto
)
648 unsigned long flags
= 0;
649 struct st_data_s
*st_gdata
;
651 pr_debug("%s: %d ", __func__
, proto
->chnl_id
);
653 st_kim_ref(&st_gdata
, 0);
654 if (!st_gdata
|| proto
->chnl_id
>= ST_MAX_CHANNELS
) {
655 pr_err(" chnl_id %d not supported", proto
->chnl_id
);
656 return -EPROTONOSUPPORT
;
659 spin_lock_irqsave(&st_gdata
->lock
, flags
);
661 if (st_gdata
->is_registered
[proto
->chnl_id
] == false) {
662 pr_err(" chnl_id %d not registered", proto
->chnl_id
);
663 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
664 return -EPROTONOSUPPORT
;
667 if (st_gdata
->protos_registered
)
668 st_gdata
->protos_registered
--;
670 remove_channel_from_table(st_gdata
, proto
);
671 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
673 if ((st_gdata
->protos_registered
== ST_EMPTY
) &&
674 (!test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
675 pr_info(" all chnl_ids unregistered ");
677 /* stop traffic on tty */
679 tty_ldisc_flush(st_gdata
->tty
);
680 stop_tty(st_gdata
->tty
);
683 /* all chnl_ids now unregistered */
684 st_kim_stop(st_gdata
->kim_data
);
686 st_ll_disable(st_gdata
);
692 * called in protocol stack drivers
693 * via the write function pointer
695 long st_write(struct sk_buff
*skb
)
697 struct st_data_s
*st_gdata
;
700 st_kim_ref(&st_gdata
, 0);
701 if (unlikely(skb
== NULL
|| st_gdata
== NULL
702 || st_gdata
->tty
== NULL
)) {
703 pr_err("data/tty unavailable to perform write");
707 pr_debug("%d to be written", skb
->len
);
710 /* st_ll to decide where to enqueue the skb */
711 st_int_enqueue(st_gdata
, skb
);
713 st_tx_wakeup(st_gdata
);
715 /* return number of bytes written */
719 /* for protocols making use of shared transport */
720 EXPORT_SYMBOL_GPL(st_unregister
);
722 /********************************************************************/
724 * functions called from TTY layer
726 static int st_tty_open(struct tty_struct
*tty
)
728 struct st_data_s
*st_gdata
;
729 pr_info("%s ", __func__
);
731 st_kim_ref(&st_gdata
, 0);
733 tty
->disc_data
= st_gdata
;
735 /* don't do an wakeup for now */
736 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
738 /* mem already allocated
740 tty
->receive_room
= 65536;
741 /* Flush any pending characters in the driver and discipline. */
742 tty_ldisc_flush(tty
);
743 tty_driver_flush_buffer(tty
);
745 * signal to UIM via KIM that -
746 * installation of N_TI_WL ldisc is complete
748 st_kim_complete(st_gdata
->kim_data
);
749 pr_debug("done %s", __func__
);
754 static void st_tty_close(struct tty_struct
*tty
)
758 struct st_data_s
*st_gdata
= tty
->disc_data
;
760 pr_info("%s ", __func__
);
764 * if a protocol has been registered & line discipline
765 * un-installed for some reason - what should be done ?
767 spin_lock_irqsave(&st_gdata
->lock
, flags
);
768 for (i
= ST_BT
; i
< ST_MAX_CHANNELS
; i
++) {
769 if (st_gdata
->is_registered
[i
] == true)
770 pr_err("%d not un-registered", i
);
771 st_gdata
->list
[i
] = NULL
;
772 st_gdata
->is_registered
[i
] = false;
774 st_gdata
->protos_registered
= 0;
775 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
777 * signal to UIM via KIM that -
778 * N_TI_WL ldisc is un-installed
780 st_kim_complete(st_gdata
->kim_data
);
781 st_gdata
->tty
= NULL
;
782 /* Flush any pending characters in the driver and discipline. */
783 tty_ldisc_flush(tty
);
784 tty_driver_flush_buffer(tty
);
786 spin_lock_irqsave(&st_gdata
->lock
, flags
);
787 /* empty out txq and tx_waitq */
788 skb_queue_purge(&st_gdata
->txq
);
789 skb_queue_purge(&st_gdata
->tx_waitq
);
790 /* reset the TTY Rx states of ST */
791 st_gdata
->rx_count
= 0;
792 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
793 kfree_skb(st_gdata
->rx_skb
);
794 st_gdata
->rx_skb
= NULL
;
795 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
797 pr_debug("%s: done ", __func__
);
800 static void st_tty_receive(struct tty_struct
*tty
, const unsigned char *data
,
801 char *tty_flags
, int count
)
804 print_hex_dump(KERN_DEBUG
, ">in>", DUMP_PREFIX_NONE
,
805 16, 1, data
, count
, 0);
809 * if fw download is in progress then route incoming data
810 * to KIM for validation
812 st_recv(tty
->disc_data
, data
, count
);
813 pr_debug("done %s", __func__
);
817 * wake-up function called in from the TTY layer
818 * inside the internal wakeup function will be called
820 static void st_tty_wakeup(struct tty_struct
*tty
)
822 struct st_data_s
*st_gdata
= tty
->disc_data
;
823 pr_debug("%s ", __func__
);
824 /* don't do an wakeup for now */
825 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
828 * schedule the internal wakeup instead of calling directly to
829 * avoid lockup (port->lock needed in tty->ops->write is
832 schedule_work(&st_gdata
->work_write_wakeup
);
835 static void st_tty_flush_buffer(struct tty_struct
*tty
)
837 struct st_data_s
*st_gdata
= tty
->disc_data
;
838 pr_debug("%s ", __func__
);
840 kfree_skb(st_gdata
->tx_skb
);
841 st_gdata
->tx_skb
= NULL
;
843 tty_driver_flush_buffer(tty
);
847 static struct tty_ldisc_ops st_ldisc_ops
= {
848 .magic
= TTY_LDISC_MAGIC
,
851 .close
= st_tty_close
,
852 .receive_buf
= st_tty_receive
,
853 .write_wakeup
= st_tty_wakeup
,
854 .flush_buffer
= st_tty_flush_buffer
,
858 /********************************************************************/
859 int st_core_init(struct st_data_s
**core_data
)
861 struct st_data_s
*st_gdata
;
864 err
= tty_register_ldisc(N_TI_WL
, &st_ldisc_ops
);
866 pr_err("error registering %d line discipline %ld",
870 pr_debug("registered n_shared line discipline");
872 st_gdata
= kzalloc(sizeof(struct st_data_s
), GFP_KERNEL
);
874 pr_err("memory allocation failed");
875 err
= tty_unregister_ldisc(N_TI_WL
);
877 pr_err("unable to un-register ldisc %ld", err
);
882 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
883 * will be pushed in this queue for actual transmission.
885 skb_queue_head_init(&st_gdata
->txq
);
886 skb_queue_head_init(&st_gdata
->tx_waitq
);
888 /* Locking used in st_int_enqueue() to avoid multiple execution */
889 spin_lock_init(&st_gdata
->lock
);
891 err
= st_ll_init(st_gdata
);
893 pr_err("error during st_ll initialization(%ld)", err
);
895 err
= tty_unregister_ldisc(N_TI_WL
);
897 pr_err("unable to un-register ldisc");
901 INIT_WORK(&st_gdata
->work_write_wakeup
, work_fn_write_wakeup
);
903 *core_data
= st_gdata
;
907 void st_core_exit(struct st_data_s
*st_gdata
)
910 /* internal module cleanup */
911 err
= st_ll_deinit(st_gdata
);
913 pr_err("error during deinit of ST LL %ld", err
);
915 if (st_gdata
!= NULL
) {
916 /* Free ST Tx Qs and skbs */
917 skb_queue_purge(&st_gdata
->txq
);
918 skb_queue_purge(&st_gdata
->tx_waitq
);
919 kfree_skb(st_gdata
->rx_skb
);
920 kfree_skb(st_gdata
->tx_skb
);
921 /* TTY ldisc cleanup */
922 err
= tty_unregister_ldisc(N_TI_WL
);
924 pr_err("unable to un-register ldisc %ld", err
);
925 /* free the global data pointer */