2 * Shared Transport Line discipline driver Core
3 * This hooks up ST KIM driver and ST LL driver
4 * Copyright (C) 2009-2010 Texas Instruments
5 * Author: Pavan Savoy <pavan_savoy@ti.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define pr_fmt(fmt) "(stc): " fmt
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/tty.h>
28 /* understand BT, FM and GPS for now */
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31 #include <net/bluetooth/hci.h>
32 #include <linux/ti_wilink_st.h>
34 /* function pointer pointing to either,
35 * st_kim_recv during registration to receive fw download responses
36 * st_int_recv after registration to receive proto stack responses
38 void (*st_recv
) (void*, const unsigned char*, long);
40 /********************************************************************/
42 /* internal misc functions */
43 bool is_protocol_list_empty(void)
46 pr_debug(" %s ", __func__
);
47 for (i
= 0; i
< ST_MAX
; i
++) {
48 if (st_gdata
->list
[i
] != NULL
)
57 /* can be called in from
58 * -- KIM (during fw download)
59 * -- ST Core (during st_write)
61 * This is the internal write function - a wrapper
64 int st_int_write(struct st_data_s
*st_gdata
,
65 const unsigned char *data
, int count
)
67 struct tty_struct
*tty
;
68 if (unlikely(st_gdata
== NULL
|| st_gdata
->tty
== NULL
)) {
69 pr_err("tty unavailable to perform write");
74 print_hex_dump(KERN_DEBUG
, "<out<", DUMP_PREFIX_NONE
,
75 16, 1, data
, count
, 0);
77 return tty
->ops
->write(tty
, data
, count
);
82 * push the skb received to relevant
85 void st_send_frame(enum proto_type protoid
, struct st_data_s
*st_gdata
)
87 pr_info(" %s(prot:%d) ", __func__
, protoid
);
90 (st_gdata
== NULL
|| st_gdata
->rx_skb
== NULL
91 || st_gdata
->list
[protoid
] == NULL
)) {
92 pr_err("protocol %d not registered, no data to send?",
94 kfree_skb(st_gdata
->rx_skb
);
98 * this shouldn't take long
99 * - should be just skb_queue_tail for the
100 * protocol stack driver
102 if (likely(st_gdata
->list
[protoid
]->recv
!= NULL
)) {
104 (st_gdata
->list
[protoid
]->recv
105 (st_gdata
->list
[protoid
]->priv_data
, st_gdata
->rx_skb
)
107 pr_err(" proto stack %d's ->recv failed", protoid
);
108 kfree_skb(st_gdata
->rx_skb
);
112 pr_err(" proto stack %d's ->recv null", protoid
);
113 kfree_skb(st_gdata
->rx_skb
);
120 * to call registration complete callbacks
121 * of all protocol stack drivers
123 void st_reg_complete(struct st_data_s
*st_gdata
, char err
)
126 pr_info(" %s ", __func__
);
127 for (i
= 0; i
< ST_MAX
; i
++) {
128 if (likely(st_gdata
!= NULL
&& st_gdata
->list
[i
] != NULL
&&
129 st_gdata
->list
[i
]->reg_complete_cb
!= NULL
))
130 st_gdata
->list
[i
]->reg_complete_cb
131 (st_gdata
->list
[i
]->priv_data
, err
);
135 static inline int st_check_data_len(struct st_data_s
*st_gdata
,
136 int protoid
, int len
)
138 int room
= skb_tailroom(st_gdata
->rx_skb
);
140 pr_debug("len %d room %d", len
, room
);
143 /* Received packet has only packet header and
144 * has zero length payload. So, ask ST CORE to
145 * forward the packet to protocol driver (BT/FM/GPS)
147 st_send_frame(protoid
, st_gdata
);
149 } else if (len
> room
) {
150 /* Received packet's payload length is larger.
151 * We can't accommodate it in created skb.
153 pr_err("Data length is too large len %d room %d", len
,
155 kfree_skb(st_gdata
->rx_skb
);
157 /* Packet header has non-zero payload length and
158 * we have enough space in created skb. Lets read
160 st_gdata
->rx_state
= ST_BT_W4_DATA
;
161 st_gdata
->rx_count
= len
;
165 /* Change ST state to continue to process next
167 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
168 st_gdata
->rx_skb
= NULL
;
169 st_gdata
->rx_count
= 0;
175 * st_wakeup_ack - internal function for action when wake-up ack
178 static inline void st_wakeup_ack(struct st_data_s
*st_gdata
,
181 struct sk_buff
*waiting_skb
;
182 unsigned long flags
= 0;
184 spin_lock_irqsave(&st_gdata
->lock
, flags
);
185 /* de-Q from waitQ and Q in txQ now that the
188 while ((waiting_skb
= skb_dequeue(&st_gdata
->tx_waitq
)))
189 skb_queue_tail(&st_gdata
->txq
, waiting_skb
);
191 /* state forwarded to ST LL */
192 st_ll_sleep_state(st_gdata
, (unsigned long)cmd
);
193 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
195 /* wake up to send the recently copied skbs from waitQ */
196 st_tx_wakeup(st_gdata
);
200 * st_int_recv - ST's internal receive function.
201 * Decodes received RAW data and forwards to corresponding
202 * client drivers (Bluetooth,FM,GPS..etc).
203 * This can receive various types of packets,
204 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
205 * CH-8 packets from FM, CH-9 packets from GPS cores.
207 void st_int_recv(void *disc_data
,
208 const unsigned char *data
, long count
)
211 struct hci_event_hdr
*eh
;
212 struct hci_acl_hdr
*ah
;
213 struct hci_sco_hdr
*sh
;
214 struct fm_event_hdr
*fm
;
215 struct gps_event_hdr
*gps
;
216 int len
= 0, type
= 0, dlen
= 0;
217 static enum proto_type protoid
= ST_MAX
;
218 struct st_data_s
*st_gdata
= (struct st_data_s
*)disc_data
;
221 /* tty_receive sent null ? */
222 if (unlikely(ptr
== NULL
) || (st_gdata
== NULL
)) {
223 pr_err(" received null from TTY ");
227 pr_info("count %ld rx_state %ld"
228 "rx_count %ld", count
, st_gdata
->rx_state
,
231 /* Decode received bytes here */
233 if (st_gdata
->rx_count
) {
234 len
= min_t(unsigned int, st_gdata
->rx_count
, count
);
235 memcpy(skb_put(st_gdata
->rx_skb
, len
), ptr
, len
);
236 st_gdata
->rx_count
-= len
;
240 if (st_gdata
->rx_count
)
243 /* Check ST RX state machine , where are we? */
244 switch (st_gdata
->rx_state
) {
246 /* Waiting for complete packet ? */
248 pr_debug("Complete pkt received");
250 /* Ask ST CORE to forward
251 * the packet to protocol driver */
252 st_send_frame(protoid
, st_gdata
);
254 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
255 st_gdata
->rx_skb
= NULL
;
256 protoid
= ST_MAX
; /* is this required ? */
259 /* Waiting for Bluetooth event header ? */
260 case ST_BT_W4_EVENT_HDR
:
261 eh
= (struct hci_event_hdr
*)st_gdata
->rx_skb
->
264 pr_debug("Event header: evt 0x%2.2x"
265 "plen %d", eh
->evt
, eh
->plen
);
267 st_check_data_len(st_gdata
, protoid
, eh
->plen
);
270 /* Waiting for Bluetooth acl header ? */
271 case ST_BT_W4_ACL_HDR
:
272 ah
= (struct hci_acl_hdr
*)st_gdata
->rx_skb
->
274 dlen
= __le16_to_cpu(ah
->dlen
);
276 pr_info("ACL header: dlen %d", dlen
);
278 st_check_data_len(st_gdata
, protoid
, dlen
);
281 /* Waiting for Bluetooth sco header ? */
282 case ST_BT_W4_SCO_HDR
:
283 sh
= (struct hci_sco_hdr
*)st_gdata
->rx_skb
->
286 pr_info("SCO header: dlen %d", sh
->dlen
);
288 st_check_data_len(st_gdata
, protoid
, sh
->dlen
);
290 case ST_FM_W4_EVENT_HDR
:
291 fm
= (struct fm_event_hdr
*)st_gdata
->rx_skb
->
293 pr_info("FM Header: ");
294 st_check_data_len(st_gdata
, ST_FM
, fm
->plen
);
296 /* TODO : Add GPS packet machine logic here */
297 case ST_GPS_W4_EVENT_HDR
:
298 /* [0x09 pkt hdr][R/W byte][2 byte len] */
299 gps
= (struct gps_event_hdr
*)st_gdata
->rx_skb
->
301 pr_info("GPS Header: ");
302 st_check_data_len(st_gdata
, ST_GPS
, gps
->plen
);
304 } /* end of switch rx_state */
307 /* end of if rx_count */
308 /* Check first byte of packet and identify module
309 * owner (BT/FM/GPS) */
312 /* Bluetooth event packet? */
314 pr_info("Event packet");
315 st_gdata
->rx_state
= ST_BT_W4_EVENT_HDR
;
316 st_gdata
->rx_count
= HCI_EVENT_HDR_SIZE
;
317 type
= HCI_EVENT_PKT
;
321 /* Bluetooth acl packet? */
322 case HCI_ACLDATA_PKT
:
323 pr_info("ACL packet");
324 st_gdata
->rx_state
= ST_BT_W4_ACL_HDR
;
325 st_gdata
->rx_count
= HCI_ACL_HDR_SIZE
;
326 type
= HCI_ACLDATA_PKT
;
330 /* Bluetooth sco packet? */
331 case HCI_SCODATA_PKT
:
332 pr_info("SCO packet");
333 st_gdata
->rx_state
= ST_BT_W4_SCO_HDR
;
334 st_gdata
->rx_count
= HCI_SCO_HDR_SIZE
;
335 type
= HCI_SCODATA_PKT
;
339 /* Channel 8(FM) packet? */
341 pr_info("FM CH8 packet");
342 type
= ST_FM_CH8_PKT
;
343 st_gdata
->rx_state
= ST_FM_W4_EVENT_HDR
;
344 st_gdata
->rx_count
= FM_EVENT_HDR_SIZE
;
348 /* Channel 9(GPS) packet? */
349 case 0x9: /*ST_LL_GPS_CH9_PKT */
350 pr_info("GPS CH9 packet");
351 type
= 0x9; /* ST_LL_GPS_CH9_PKT; */
353 st_gdata
->rx_state
= ST_GPS_W4_EVENT_HDR
;
354 st_gdata
->rx_count
= 3; /* GPS_EVENT_HDR_SIZE -1*/
359 pr_info("PM packet");
360 /* this takes appropriate action based on
361 * sleep state received --
363 st_ll_sleep_state(st_gdata
, *ptr
);
368 pr_info("PM packet");
369 /* wake up ack received */
370 st_wakeup_ack(st_gdata
, *ptr
);
376 pr_err("Unknown packet type %2.2x", (__u8
) *ptr
);
386 /* Allocate new packet to hold received data */
388 bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
389 if (!st_gdata
->rx_skb
) {
390 pr_err("Can't allocate mem for new packet");
391 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
392 st_gdata
->rx_count
= 0;
395 bt_cb(st_gdata
->rx_skb
)->pkt_type
= type
;
397 case ST_FM
: /* for FM */
399 alloc_skb(FM_MAX_FRAME_SIZE
, GFP_ATOMIC
);
400 if (!st_gdata
->rx_skb
) {
401 pr_err("Can't allocate mem for new packet");
402 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
403 st_gdata
->rx_count
= 0;
406 /* place holder 0x08 */
407 skb_reserve(st_gdata
->rx_skb
, 1);
408 st_gdata
->rx_skb
->cb
[0] = ST_FM_CH8_PKT
;
413 alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC
);
414 if (!st_gdata
->rx_skb
) {
415 pr_err("Can't allocate mem for new packet");
416 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
417 st_gdata
->rx_count
= 0;
420 /* place holder 0x09 */
421 skb_reserve(st_gdata
->rx_skb
, 1);
422 st_gdata
->rx_skb
->cb
[0] = 0x09; /*ST_GPS_CH9_PKT; */
428 pr_debug("done %s", __func__
);
433 * st_int_dequeue - internal de-Q function.
434 * If the previous data set was not written
435 * completely, return that skb which has the pending data.
436 * In normal cases, return top of txq.
438 struct sk_buff
*st_int_dequeue(struct st_data_s
*st_gdata
)
440 struct sk_buff
*returning_skb
;
442 pr_debug("%s", __func__
);
443 if (st_gdata
->tx_skb
!= NULL
) {
444 returning_skb
= st_gdata
->tx_skb
;
445 st_gdata
->tx_skb
= NULL
;
446 return returning_skb
;
448 return skb_dequeue(&st_gdata
->txq
);
452 * st_int_enqueue - internal Q-ing function.
453 * Will either Q the skb to txq or the tx_waitq
454 * depending on the ST LL state.
455 * If the chip is asleep, then Q it onto waitq and
457 * txq and waitq needs protection since the other contexts
458 * may be sending data, waking up chip.
460 void st_int_enqueue(struct st_data_s
*st_gdata
, struct sk_buff
*skb
)
462 unsigned long flags
= 0;
464 pr_debug("%s", __func__
);
465 spin_lock_irqsave(&st_gdata
->lock
, flags
);
467 switch (st_ll_getstate(st_gdata
)) {
469 pr_info("ST LL is AWAKE, sending normally");
470 skb_queue_tail(&st_gdata
->txq
, skb
);
472 case ST_LL_ASLEEP_TO_AWAKE
:
473 skb_queue_tail(&st_gdata
->tx_waitq
, skb
);
475 case ST_LL_AWAKE_TO_ASLEEP
:
476 pr_err("ST LL is illegal state(%ld),"
477 "purging received skb.", st_ll_getstate(st_gdata
));
481 skb_queue_tail(&st_gdata
->tx_waitq
, skb
);
482 st_ll_wakeup(st_gdata
);
485 pr_err("ST LL is illegal state(%ld),"
486 "purging received skb.", st_ll_getstate(st_gdata
));
491 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
492 pr_debug("done %s", __func__
);
497 * internal wakeup function
499 * - TTY layer when write's finished
500 * - st_write (in context of the protocol stack)
502 void st_tx_wakeup(struct st_data_s
*st_data
)
505 unsigned long flags
; /* for irq save flags */
506 pr_debug("%s", __func__
);
507 /* check for sending & set flag sending here */
508 if (test_and_set_bit(ST_TX_SENDING
, &st_data
->tx_state
)) {
509 pr_info("ST already sending");
511 set_bit(ST_TX_WAKEUP
, &st_data
->tx_state
);
513 /* TX_WAKEUP will be checked in another
517 do { /* come back if st_tx_wakeup is set */
518 /* woke-up to write */
519 clear_bit(ST_TX_WAKEUP
, &st_data
->tx_state
);
520 while ((skb
= st_int_dequeue(st_data
))) {
522 spin_lock_irqsave(&st_data
->lock
, flags
);
523 /* enable wake-up from TTY */
524 set_bit(TTY_DO_WRITE_WAKEUP
, &st_data
->tty
->flags
);
525 len
= st_int_write(st_data
, skb
->data
, skb
->len
);
527 /* if skb->len = len as expected, skb->len=0 */
529 /* would be the next skb to be sent */
530 st_data
->tx_skb
= skb
;
531 spin_unlock_irqrestore(&st_data
->lock
, flags
);
535 spin_unlock_irqrestore(&st_data
->lock
, flags
);
537 /* if wake-up is set in another context- restart sending */
538 } while (test_bit(ST_TX_WAKEUP
, &st_data
->tx_state
));
540 /* clear flag sending */
541 clear_bit(ST_TX_SENDING
, &st_data
->tx_state
);
544 /********************************************************************/
545 /* functions called from ST KIM
547 void kim_st_list_protocols(struct st_data_s
*st_gdata
, void *buf
)
549 seq_printf(buf
, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
550 st_gdata
->protos_registered
,
551 st_gdata
->list
[ST_BT
] != NULL
? 'R' : 'U',
552 st_gdata
->list
[ST_FM
] != NULL
? 'R' : 'U',
553 st_gdata
->list
[ST_GPS
] != NULL
? 'R' : 'U');
556 /********************************************************************/
558 * functions called from protocol stack drivers
561 long st_register(struct st_proto_s
*new_proto
)
563 struct st_data_s
*st_gdata
;
565 unsigned long flags
= 0;
567 st_kim_ref(&st_gdata
, 0);
568 pr_info("%s(%d) ", __func__
, new_proto
->type
);
569 if (st_gdata
== NULL
|| new_proto
== NULL
|| new_proto
->recv
== NULL
570 || new_proto
->reg_complete_cb
== NULL
) {
571 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
575 if (new_proto
->type
< ST_BT
|| new_proto
->type
>= ST_MAX
) {
576 pr_err("protocol %d not supported", new_proto
->type
);
577 return -EPROTONOSUPPORT
;
580 if (st_gdata
->list
[new_proto
->type
] != NULL
) {
581 pr_err("protocol %d already registered", new_proto
->type
);
585 /* can be from process context only */
586 spin_lock_irqsave(&st_gdata
->lock
, flags
);
588 if (test_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
)) {
589 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto
->type
);
590 /* fw download in progress */
591 st_kim_chip_toggle(new_proto
->type
, KIM_GPIO_ACTIVE
);
593 st_gdata
->list
[new_proto
->type
] = new_proto
;
594 st_gdata
->protos_registered
++;
595 new_proto
->write
= st_write
;
597 set_bit(ST_REG_PENDING
, &st_gdata
->st_state
);
598 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
600 } else if (st_gdata
->protos_registered
== ST_EMPTY
) {
601 pr_info(" protocol list empty :%d ", new_proto
->type
);
602 set_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
603 st_recv
= st_kim_recv
;
605 /* release lock previously held - re-locked below */
606 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
608 /* enable the ST LL - to set default chip state */
609 st_ll_enable(st_gdata
);
610 /* this may take a while to complete
611 * since it involves BT fw download
613 err
= st_kim_start(st_gdata
->kim_data
);
615 clear_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
616 if ((st_gdata
->protos_registered
!= ST_EMPTY
) &&
617 (test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
618 pr_err(" KIM failure complete callback ");
619 st_reg_complete(st_gdata
, -1);
625 /* the protocol might require other gpios to be toggled
627 st_kim_chip_toggle(new_proto
->type
, KIM_GPIO_ACTIVE
);
629 clear_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
630 st_recv
= st_int_recv
;
632 /* this is where all pending registration
633 * are signalled to be complete by calling callback functions
635 if ((st_gdata
->protos_registered
!= ST_EMPTY
) &&
636 (test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
637 pr_debug(" call reg complete callback ");
638 st_reg_complete(st_gdata
, 0);
640 clear_bit(ST_REG_PENDING
, &st_gdata
->st_state
);
642 /* check for already registered once more,
643 * since the above check is old
645 if (st_gdata
->list
[new_proto
->type
] != NULL
) {
646 pr_err(" proto %d already registered ",
651 spin_lock_irqsave(&st_gdata
->lock
, flags
);
652 st_gdata
->list
[new_proto
->type
] = new_proto
;
653 st_gdata
->protos_registered
++;
654 new_proto
->write
= st_write
;
655 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
658 /* if fw is already downloaded & new stack registers protocol */
660 switch (new_proto
->type
) {
666 st_kim_chip_toggle(new_proto
->type
, KIM_GPIO_ACTIVE
);
670 pr_err("%d protocol not supported",
672 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
673 return -EPROTONOSUPPORT
;
675 st_gdata
->list
[new_proto
->type
] = new_proto
;
676 st_gdata
->protos_registered
++;
677 new_proto
->write
= st_write
;
679 /* lock already held before entering else */
680 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
683 pr_debug("done %s(%d) ", __func__
, new_proto
->type
);
685 EXPORT_SYMBOL_GPL(st_register
);
687 /* to unregister a protocol -
688 * to be called from protocol stack driver
690 long st_unregister(enum proto_type type
)
693 unsigned long flags
= 0;
694 struct st_data_s
*st_gdata
;
696 pr_debug("%s: %d ", __func__
, type
);
698 st_kim_ref(&st_gdata
, 0);
699 if (type
< ST_BT
|| type
>= ST_MAX
) {
700 pr_err(" protocol %d not supported", type
);
701 return -EPROTONOSUPPORT
;
704 spin_lock_irqsave(&st_gdata
->lock
, flags
);
706 if (st_gdata
->list
[type
] == NULL
) {
707 pr_err(" protocol %d not registered", type
);
708 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
709 return -EPROTONOSUPPORT
;
712 st_gdata
->protos_registered
--;
713 st_gdata
->list
[type
] = NULL
;
715 /* kim ignores BT in the below function
716 * and handles the rest, BT is toggled
717 * only in kim_start and kim_stop
719 st_kim_chip_toggle(type
, KIM_GPIO_INACTIVE
);
720 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
722 if ((st_gdata
->protos_registered
== ST_EMPTY
) &&
723 (!test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
724 pr_info(" all protocols unregistered ");
726 /* stop traffic on tty */
728 tty_ldisc_flush(st_gdata
->tty
);
729 stop_tty(st_gdata
->tty
);
732 /* all protocols now unregistered */
733 st_kim_stop(st_gdata
->kim_data
);
735 st_ll_disable(st_gdata
);
741 * called in protocol stack drivers
742 * via the write function pointer
744 long st_write(struct sk_buff
*skb
)
746 struct st_data_s
*st_gdata
;
748 enum proto_type protoid
= ST_MAX
;
752 st_kim_ref(&st_gdata
, 0);
753 if (unlikely(skb
== NULL
|| st_gdata
== NULL
754 || st_gdata
->tty
== NULL
)) {
755 pr_err("data/tty unavailable to perform write");
758 #ifdef DEBUG /* open-up skb to read the 1st byte */
759 switch (skb
->data
[0]) {
760 case HCI_COMMAND_PKT
:
761 case HCI_ACLDATA_PKT
:
762 case HCI_SCODATA_PKT
:
772 if (unlikely(st_gdata
->list
[protoid
] == NULL
)) {
773 pr_err(" protocol %d not registered, and writing? ",
778 pr_debug("%d to be written", skb
->len
);
781 /* st_ll to decide where to enqueue the skb */
782 st_int_enqueue(st_gdata
, skb
);
784 st_tx_wakeup(st_gdata
);
786 /* return number of bytes written */
790 /* for protocols making use of shared transport */
791 EXPORT_SYMBOL_GPL(st_unregister
);
793 /********************************************************************/
795 * functions called from TTY layer
797 static int st_tty_open(struct tty_struct
*tty
)
800 struct st_data_s
*st_gdata
;
801 pr_info("%s ", __func__
);
803 st_kim_ref(&st_gdata
, 0);
805 tty
->disc_data
= st_gdata
;
807 /* don't do an wakeup for now */
808 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
810 /* mem already allocated
812 tty
->receive_room
= 65536;
813 /* Flush any pending characters in the driver and discipline. */
814 tty_ldisc_flush(tty
);
815 tty_driver_flush_buffer(tty
);
817 * signal to UIM via KIM that -
818 * installation of N_TI_WL ldisc is complete
820 st_kim_complete(st_gdata
->kim_data
);
821 pr_debug("done %s", __func__
);
825 static void st_tty_close(struct tty_struct
*tty
)
827 unsigned char i
= ST_MAX
;
828 unsigned long flags
= 0;
829 struct st_data_s
*st_gdata
= tty
->disc_data
;
831 pr_info("%s ", __func__
);
834 * if a protocol has been registered & line discipline
835 * un-installed for some reason - what should be done ?
837 spin_lock_irqsave(&st_gdata
->lock
, flags
);
838 for (i
= ST_BT
; i
< ST_MAX
; i
++) {
839 if (st_gdata
->list
[i
] != NULL
)
840 pr_err("%d not un-registered", i
);
841 st_gdata
->list
[i
] = NULL
;
843 st_gdata
->protos_registered
= 0;
844 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
846 * signal to UIM via KIM that -
847 * N_TI_WL ldisc is un-installed
849 st_kim_complete(st_gdata
->kim_data
);
850 st_gdata
->tty
= NULL
;
851 /* Flush any pending characters in the driver and discipline. */
852 tty_ldisc_flush(tty
);
853 tty_driver_flush_buffer(tty
);
855 spin_lock_irqsave(&st_gdata
->lock
, flags
);
856 /* empty out txq and tx_waitq */
857 skb_queue_purge(&st_gdata
->txq
);
858 skb_queue_purge(&st_gdata
->tx_waitq
);
859 /* reset the TTY Rx states of ST */
860 st_gdata
->rx_count
= 0;
861 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
862 kfree_skb(st_gdata
->rx_skb
);
863 st_gdata
->rx_skb
= NULL
;
864 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
866 pr_debug("%s: done ", __func__
);
869 static void st_tty_receive(struct tty_struct
*tty
, const unsigned char *data
,
870 char *tty_flags
, int count
)
874 print_hex_dump(KERN_DEBUG
, ">in>", DUMP_PREFIX_NONE
,
875 16, 1, data
, count
, 0);
879 * if fw download is in progress then route incoming data
880 * to KIM for validation
882 st_recv(tty
->disc_data
, data
, count
);
883 pr_debug("done %s", __func__
);
886 /* wake-up function called in from the TTY layer
887 * inside the internal wakeup function will be called
889 static void st_tty_wakeup(struct tty_struct
*tty
)
891 struct st_data_s
*st_gdata
= tty
->disc_data
;
892 pr_debug("%s ", __func__
);
893 /* don't do an wakeup for now */
894 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
896 /* call our internal wakeup */
897 st_tx_wakeup((void *)st_gdata
);
900 static void st_tty_flush_buffer(struct tty_struct
*tty
)
902 struct st_data_s
*st_gdata
= tty
->disc_data
;
903 pr_debug("%s ", __func__
);
905 kfree_skb(st_gdata
->tx_skb
);
906 st_gdata
->tx_skb
= NULL
;
908 tty
->ops
->flush_buffer(tty
);
912 static struct tty_ldisc_ops st_ldisc_ops
= {
913 .magic
= TTY_LDISC_MAGIC
,
916 .close
= st_tty_close
,
917 .receive_buf
= st_tty_receive
,
918 .write_wakeup
= st_tty_wakeup
,
919 .flush_buffer
= st_tty_flush_buffer
,
923 /********************************************************************/
924 int st_core_init(struct st_data_s
**core_data
)
926 struct st_data_s
*st_gdata
;
929 err
= tty_register_ldisc(N_TI_WL
, &st_ldisc_ops
);
931 pr_err("error registering %d line discipline %ld",
935 pr_debug("registered n_shared line discipline");
937 st_gdata
= kzalloc(sizeof(struct st_data_s
), GFP_KERNEL
);
939 pr_err("memory allocation failed");
940 err
= tty_unregister_ldisc(N_TI_WL
);
942 pr_err("unable to un-register ldisc %ld", err
);
947 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
948 * will be pushed in this queue for actual transmission.
950 skb_queue_head_init(&st_gdata
->txq
);
951 skb_queue_head_init(&st_gdata
->tx_waitq
);
953 /* Locking used in st_int_enqueue() to avoid multiple execution */
954 spin_lock_init(&st_gdata
->lock
);
956 err
= st_ll_init(st_gdata
);
958 pr_err("error during st_ll initialization(%ld)", err
);
960 err
= tty_unregister_ldisc(N_TI_WL
);
962 pr_err("unable to un-register ldisc");
965 *core_data
= st_gdata
;
969 void st_core_exit(struct st_data_s
*st_gdata
)
972 /* internal module cleanup */
973 err
= st_ll_deinit(st_gdata
);
975 pr_err("error during deinit of ST LL %ld", err
);
977 if (st_gdata
!= NULL
) {
978 /* Free ST Tx Qs and skbs */
979 skb_queue_purge(&st_gdata
->txq
);
980 skb_queue_purge(&st_gdata
->tx_waitq
);
981 kfree_skb(st_gdata
->rx_skb
);
982 kfree_skb(st_gdata
->tx_skb
);
983 /* TTY ldisc cleanup */
984 err
= tty_unregister_ldisc(N_TI_WL
);
986 pr_err("unable to un-register ldisc %ld", err
);
987 /* free the global data pointer */