2 * Shared Transport Line discipline driver Core
3 * This hooks up ST KIM driver and ST LL driver
4 * Copyright (C) 2009 Texas Instruments
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define pr_fmt(fmt) "(stc): " fmt
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/tty.h>
27 /* understand BT, FM and GPS for now */
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
30 #include <net/bluetooth/hci.h>
33 * packet formats for fm and gps
42 /* strings to be used for rfkill entries and by
43 * ST Core to be used for sysfs debug entry
45 #define PROTO_ENTRY(type, name) name
46 const unsigned char *protocol_strngs
[] = {
47 PROTO_ENTRY(ST_BT
, "Bluetooth"),
48 PROTO_ENTRY(ST_FM
, "FM"),
49 PROTO_ENTRY(ST_GPS
, "GPS"),
52 /* function pointer pointing to either,
53 * st_kim_recv during registration to receive fw download responses
54 * st_int_recv after registration to receive proto stack responses
56 void (*st_recv
) (void*, const unsigned char*, long);
58 /********************************************************************/
60 /* internal misc functions */
61 bool is_protocol_list_empty(void)
64 pr_info(" %s ", __func__
);
65 for (i
= 0; i
< ST_MAX
; i
++) {
66 if (st_gdata
->list
[i
] != NULL
)
74 /* can be called in from
75 * -- KIM (during fw download)
76 * -- ST Core (during st_write)
78 * This is the internal write function - a wrapper
81 int st_int_write(struct st_data_s
*st_gdata
,
82 const unsigned char *data
, int count
)
84 #ifdef VERBOSE /* for debug */
87 struct tty_struct
*tty
;
88 if (unlikely(st_gdata
== NULL
|| st_gdata
->tty
== NULL
)) {
89 pr_err("tty unavailable to perform write");
90 return ST_ERR_FAILURE
;
94 printk(KERN_ERR
"start data..\n");
95 for (i
= 0; i
< count
; i
++) /* no newlines for each datum */
96 printk(" %x", data
[i
]);
97 printk(KERN_ERR
"\n ..end data\n");
99 return tty
->ops
->write(tty
, data
, count
);
104 * push the skb received to relevant
107 void st_send_frame(enum proto_type protoid
, struct st_data_s
*st_gdata
)
109 pr_info(" %s(prot:%d) ", __func__
, protoid
);
112 (st_gdata
== NULL
|| st_gdata
->rx_skb
== NULL
113 || st_gdata
->list
[protoid
] == NULL
)) {
114 pr_err("protocol %d not registered, no data to send?",
116 kfree_skb(st_gdata
->rx_skb
);
120 * this shouldn't take long
121 * - should be just skb_queue_tail for the
122 * protocol stack driver
124 if (likely(st_gdata
->list
[protoid
]->recv
!= NULL
)) {
125 if (unlikely(st_gdata
->list
[protoid
]->recv(st_gdata
->rx_skb
)
127 pr_err(" proto stack %d's ->recv failed", protoid
);
128 kfree_skb(st_gdata
->rx_skb
);
132 pr_err(" proto stack %d's ->recv null", protoid
);
133 kfree_skb(st_gdata
->rx_skb
);
135 pr_info(" done %s", __func__
);
140 * to call registration complete callbacks
141 * of all protocol stack drivers
143 void st_reg_complete(struct st_data_s
*st_gdata
, char err
)
146 pr_info(" %s ", __func__
);
147 for (i
= 0; i
< ST_MAX
; i
++) {
148 if (likely(st_gdata
!= NULL
&& st_gdata
->list
[i
] != NULL
&&
149 st_gdata
->list
[i
]->reg_complete_cb
!= NULL
))
150 st_gdata
->list
[i
]->reg_complete_cb(err
);
154 static inline int st_check_data_len(struct st_data_s
*st_gdata
,
155 int protoid
, int len
)
157 register int room
= skb_tailroom(st_gdata
->rx_skb
);
159 pr_info("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(protoid
, st_gdata
);
168 } 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
);
176 /* Packet header has non-zero payload length and
177 * we have enough space in created skb. Lets read
179 st_gdata
->rx_state
= ST_BT_W4_DATA
;
180 st_gdata
->rx_count
= len
;
184 /* Change ST state to continue to process next
186 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
187 st_gdata
->rx_skb
= NULL
;
188 st_gdata
->rx_count
= 0;
193 /* internal function for action when wake-up ack
196 static inline void st_wakeup_ack(struct st_data_s
*st_gdata
,
199 register struct sk_buff
*waiting_skb
;
200 unsigned long flags
= 0;
202 spin_lock_irqsave(&st_gdata
->lock
, flags
);
203 /* de-Q from waitQ and Q in txQ now that the
206 while ((waiting_skb
= skb_dequeue(&st_gdata
->tx_waitq
)))
207 skb_queue_tail(&st_gdata
->txq
, waiting_skb
);
209 /* state forwarded to ST LL */
210 st_ll_sleep_state(st_gdata
, (unsigned long)cmd
);
211 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
213 /* wake up to send the recently copied skbs from waitQ */
214 st_tx_wakeup(st_gdata
);
217 /* Decodes received RAW data and forwards to corresponding
218 * client drivers (Bluetooth,FM,GPS..etc).
221 void st_int_recv(void *disc_data
,
222 const unsigned char *data
, long count
)
225 struct hci_event_hdr
*eh
;
226 struct hci_acl_hdr
*ah
;
227 struct hci_sco_hdr
*sh
;
228 struct fm_event_hdr
*fm
;
229 struct gps_event_hdr
*gps
;
230 register int len
= 0, type
= 0, dlen
= 0;
231 static enum proto_type protoid
= ST_MAX
;
232 struct st_data_s
*st_gdata
= (struct st_data_s
*)disc_data
;
235 /* tty_receive sent null ? */
236 if (unlikely(ptr
== NULL
) || (st_gdata
== NULL
)) {
237 pr_err(" received null from TTY ");
241 pr_info("count %ld rx_state %ld"
242 "rx_count %ld", count
, st_gdata
->rx_state
,
245 /* Decode received bytes here */
247 if (st_gdata
->rx_count
) {
248 len
= min_t(unsigned int, st_gdata
->rx_count
, count
);
249 memcpy(skb_put(st_gdata
->rx_skb
, len
), ptr
, len
);
250 st_gdata
->rx_count
-= len
;
254 if (st_gdata
->rx_count
)
257 /* Check ST RX state machine , where are we? */
258 switch (st_gdata
->rx_state
) {
260 /* Waiting for complete packet ? */
262 pr_info("Complete pkt received");
264 /* Ask ST CORE to forward
265 * the packet to protocol driver */
266 st_send_frame(protoid
, st_gdata
);
268 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
269 st_gdata
->rx_skb
= NULL
;
270 protoid
= ST_MAX
; /* is this required ? */
273 /* Waiting for Bluetooth event header ? */
274 case ST_BT_W4_EVENT_HDR
:
275 eh
= (struct hci_event_hdr
*)st_gdata
->rx_skb
->
278 pr_info("Event header: evt 0x%2.2x"
279 "plen %d", eh
->evt
, eh
->plen
);
281 st_check_data_len(st_gdata
, protoid
, eh
->plen
);
284 /* Waiting for Bluetooth acl header ? */
285 case ST_BT_W4_ACL_HDR
:
286 ah
= (struct hci_acl_hdr
*)st_gdata
->rx_skb
->
288 dlen
= __le16_to_cpu(ah
->dlen
);
290 pr_info("ACL header: dlen %d", dlen
);
292 st_check_data_len(st_gdata
, protoid
, dlen
);
295 /* Waiting for Bluetooth sco header ? */
296 case ST_BT_W4_SCO_HDR
:
297 sh
= (struct hci_sco_hdr
*)st_gdata
->rx_skb
->
300 pr_info("SCO header: dlen %d", sh
->dlen
);
302 st_check_data_len(st_gdata
, protoid
, sh
->dlen
);
304 case ST_FM_W4_EVENT_HDR
:
305 fm
= (struct fm_event_hdr
*)st_gdata
->rx_skb
->
307 pr_info("FM Header: ");
308 st_check_data_len(st_gdata
, ST_FM
, fm
->plen
);
310 /* TODO : Add GPS packet machine logic here */
311 case ST_GPS_W4_EVENT_HDR
:
312 /* [0x09 pkt hdr][R/W byte][2 byte len] */
313 gps
= (struct gps_event_hdr
*)st_gdata
->rx_skb
->
315 pr_info("GPS Header: ");
316 st_check_data_len(st_gdata
, ST_GPS
, gps
->plen
);
318 } /* end of switch rx_state */
321 /* end of if rx_count */
322 /* Check first byte of packet and identify module
323 * owner (BT/FM/GPS) */
326 /* Bluetooth event packet? */
328 pr_info("Event packet");
329 st_gdata
->rx_state
= ST_BT_W4_EVENT_HDR
;
330 st_gdata
->rx_count
= HCI_EVENT_HDR_SIZE
;
331 type
= HCI_EVENT_PKT
;
335 /* Bluetooth acl packet? */
336 case HCI_ACLDATA_PKT
:
337 pr_info("ACL packet");
338 st_gdata
->rx_state
= ST_BT_W4_ACL_HDR
;
339 st_gdata
->rx_count
= HCI_ACL_HDR_SIZE
;
340 type
= HCI_ACLDATA_PKT
;
344 /* Bluetooth sco packet? */
345 case HCI_SCODATA_PKT
:
346 pr_info("SCO packet");
347 st_gdata
->rx_state
= ST_BT_W4_SCO_HDR
;
348 st_gdata
->rx_count
= HCI_SCO_HDR_SIZE
;
349 type
= HCI_SCODATA_PKT
;
353 /* Channel 8(FM) packet? */
355 pr_info("FM CH8 packet");
356 type
= ST_FM_CH8_PKT
;
357 st_gdata
->rx_state
= ST_FM_W4_EVENT_HDR
;
358 st_gdata
->rx_count
= FM_EVENT_HDR_SIZE
;
362 /* Channel 9(GPS) packet? */
363 case 0x9: /*ST_LL_GPS_CH9_PKT */
364 pr_info("GPS CH9 packet");
365 type
= 0x9; /* ST_LL_GPS_CH9_PKT; */
367 st_gdata
->rx_state
= ST_GPS_W4_EVENT_HDR
;
368 st_gdata
->rx_count
= 3; /* GPS_EVENT_HDR_SIZE -1*/
373 pr_info("PM packet");
374 /* this takes appropriate action based on
375 * sleep state received --
377 st_ll_sleep_state(st_gdata
, *ptr
);
382 pr_info("PM packet");
383 /* wake up ack received */
384 st_wakeup_ack(st_gdata
, *ptr
);
390 pr_err("Unknown packet type %2.2x", (__u8
) *ptr
);
400 /* Allocate new packet to hold received data */
402 bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
403 if (!st_gdata
->rx_skb
) {
404 pr_err("Can't allocate mem for new packet");
405 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
406 st_gdata
->rx_count
= 0;
409 bt_cb(st_gdata
->rx_skb
)->pkt_type
= type
;
411 case ST_FM
: /* for FM */
413 alloc_skb(FM_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 0x08 */
421 skb_reserve(st_gdata
->rx_skb
, 1);
422 st_gdata
->rx_skb
->cb
[0] = ST_FM_CH8_PKT
;
427 alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC
);
428 if (!st_gdata
->rx_skb
) {
429 pr_err("Can't allocate mem for new packet");
430 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
431 st_gdata
->rx_count
= 0;
434 /* place holder 0x09 */
435 skb_reserve(st_gdata
->rx_skb
, 1);
436 st_gdata
->rx_skb
->cb
[0] = 0x09; /*ST_GPS_CH9_PKT; */
442 pr_info("done %s", __func__
);
446 /* internal de-Q function
447 * -- return previous in-completely written skb
448 * or return the skb in the txQ
450 struct sk_buff
*st_int_dequeue(struct st_data_s
*st_gdata
)
452 struct sk_buff
*returning_skb
;
454 pr_info("%s", __func__
);
455 /* if the previous skb wasn't written completely
457 if (st_gdata
->tx_skb
!= NULL
) {
458 returning_skb
= st_gdata
->tx_skb
;
459 st_gdata
->tx_skb
= NULL
;
460 return returning_skb
;
463 /* de-Q from the txQ always if previous write is complete */
464 return skb_dequeue(&st_gdata
->txq
);
467 /* internal Q-ing function
468 * will either Q the skb to txq or the tx_waitq
469 * depending on the ST LL state
471 * lock the whole func - since ll_getstate and Q-ing should happen
474 void st_int_enqueue(struct st_data_s
*st_gdata
, struct sk_buff
*skb
)
476 unsigned long flags
= 0;
478 pr_info("%s", __func__
);
479 /* this function can be invoked in more then one context.
481 spin_lock_irqsave(&st_gdata
->lock
, flags
);
483 switch (st_ll_getstate(st_gdata
)) {
485 pr_info("ST LL is AWAKE, sending normally");
486 skb_queue_tail(&st_gdata
->txq
, skb
);
488 case ST_LL_ASLEEP_TO_AWAKE
:
489 skb_queue_tail(&st_gdata
->tx_waitq
, skb
);
491 case ST_LL_AWAKE_TO_ASLEEP
: /* host cannot be in this state */
492 pr_err("ST LL is illegal state(%ld),"
493 "purging received skb.", st_ll_getstate(st_gdata
));
498 /* call a function of ST LL to put data
499 * in tx_waitQ and wake_ind in txQ
501 skb_queue_tail(&st_gdata
->tx_waitq
, skb
);
502 st_ll_wakeup(st_gdata
);
505 pr_err("ST LL is illegal state(%ld),"
506 "purging received skb.", st_ll_getstate(st_gdata
));
510 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
511 pr_info("done %s", __func__
);
516 * internal wakeup function
518 * - TTY layer when write's finished
519 * - st_write (in context of the protocol stack)
521 void st_tx_wakeup(struct st_data_s
*st_data
)
524 unsigned long flags
; /* for irq save flags */
525 pr_info("%s", __func__
);
526 /* check for sending & set flag sending here */
527 if (test_and_set_bit(ST_TX_SENDING
, &st_data
->tx_state
)) {
528 pr_info("ST already sending");
530 set_bit(ST_TX_WAKEUP
, &st_data
->tx_state
);
532 /* TX_WAKEUP will be checked in another
536 do { /* come back if st_tx_wakeup is set */
537 /* woke-up to write */
538 clear_bit(ST_TX_WAKEUP
, &st_data
->tx_state
);
539 while ((skb
= st_int_dequeue(st_data
))) {
541 spin_lock_irqsave(&st_data
->lock
, flags
);
542 /* enable wake-up from TTY */
543 set_bit(TTY_DO_WRITE_WAKEUP
, &st_data
->tty
->flags
);
544 len
= st_int_write(st_data
, skb
->data
, skb
->len
);
546 /* if skb->len = len as expected, skb->len=0 */
548 /* would be the next skb to be sent */
549 st_data
->tx_skb
= skb
;
550 spin_unlock_irqrestore(&st_data
->lock
, flags
);
554 spin_unlock_irqrestore(&st_data
->lock
, flags
);
556 /* if wake-up is set in another context- restart sending */
557 } while (test_bit(ST_TX_WAKEUP
, &st_data
->tx_state
));
559 /* clear flag sending */
560 clear_bit(ST_TX_SENDING
, &st_data
->tx_state
);
563 /********************************************************************/
564 /* functions called from ST KIM
566 void kim_st_list_protocols(struct st_data_s
*st_gdata
, char *buf
)
568 unsigned long flags
= 0;
570 unsigned char i
= ST_MAX
;
572 spin_lock_irqsave(&st_gdata
->lock
, flags
);
573 #ifdef DEBUG /* more detailed log */
574 for (i
= 0; i
< ST_MAX
; i
++) {
576 sprintf(buf
, "%s is %s", protocol_strngs
[i
],
578 NULL
? "Registered" : "Unregistered");
580 sprintf(buf
, "%s\n%s is %s", buf
, protocol_strngs
[i
],
582 NULL
? "Registered" : "Unregistered");
585 sprintf(buf
, "%s\n", buf
);
586 #else /* limited info */
587 sprintf(buf
, "BT=%c\nFM=%c\nGPS=%c\n",
588 st_gdata
->list
[ST_BT
] != NULL
? 'R' : 'U',
589 st_gdata
->list
[ST_FM
] != NULL
? 'R' : 'U',
590 st_gdata
->list
[ST_GPS
] != NULL
? 'R' : 'U');
592 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
595 /********************************************************************/
597 * functions called from protocol stack drivers
600 long st_register(struct st_proto_s
*new_proto
)
602 struct st_data_s
*st_gdata
;
603 long err
= ST_SUCCESS
;
604 unsigned long flags
= 0;
606 st_kim_ref(&st_gdata
);
607 pr_info("%s(%d) ", __func__
, new_proto
->type
);
608 if (st_gdata
== NULL
|| new_proto
== NULL
|| new_proto
->recv
== NULL
609 || new_proto
->reg_complete_cb
== NULL
) {
610 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
611 return ST_ERR_FAILURE
;
614 if (new_proto
->type
< ST_BT
|| new_proto
->type
>= ST_MAX
) {
615 pr_err("protocol %d not supported", new_proto
->type
);
616 return ST_ERR_NOPROTO
;
619 if (st_gdata
->list
[new_proto
->type
] != NULL
) {
620 pr_err("protocol %d already registered", new_proto
->type
);
621 return ST_ERR_ALREADY
;
624 /* can be from process context only */
625 spin_lock_irqsave(&st_gdata
->lock
, flags
);
627 if (test_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
)) {
628 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto
->type
);
629 /* fw download in progress */
630 st_kim_chip_toggle(new_proto
->type
, KIM_GPIO_ACTIVE
);
632 st_gdata
->list
[new_proto
->type
] = new_proto
;
633 new_proto
->write
= st_write
;
635 set_bit(ST_REG_PENDING
, &st_gdata
->st_state
);
636 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
637 return ST_ERR_PENDING
;
638 } else if (st_gdata
->protos_registered
== ST_EMPTY
) {
639 pr_info(" protocol list empty :%d ", new_proto
->type
);
640 set_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
641 st_recv
= st_kim_recv
;
643 /* release lock previously held - re-locked below */
644 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
646 /* enable the ST LL - to set default chip state */
647 st_ll_enable(st_gdata
);
648 /* this may take a while to complete
649 * since it involves BT fw download
651 err
= st_kim_start();
652 if (err
!= ST_SUCCESS
) {
653 clear_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
654 if ((st_gdata
->protos_registered
!= ST_EMPTY
) &&
655 (test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
656 pr_err(" KIM failure complete callback ");
657 st_reg_complete(st_gdata
, ST_ERR_FAILURE
);
660 return ST_ERR_FAILURE
;
663 /* the protocol might require other gpios to be toggled
665 st_kim_chip_toggle(new_proto
->type
, KIM_GPIO_ACTIVE
);
667 clear_bit(ST_REG_IN_PROGRESS
, &st_gdata
->st_state
);
668 st_recv
= st_int_recv
;
670 /* this is where all pending registration
671 * are signalled to be complete by calling callback functions
673 if ((st_gdata
->protos_registered
!= ST_EMPTY
) &&
674 (test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
675 pr_info(" call reg complete callback ");
676 st_gdata
->protos_registered
++;
677 st_reg_complete(st_gdata
, ST_SUCCESS
);
679 clear_bit(ST_REG_PENDING
, &st_gdata
->st_state
);
681 /* check for already registered once more,
682 * since the above check is old
684 if (st_gdata
->list
[new_proto
->type
] != NULL
) {
685 pr_err(" proto %d already registered ",
687 return ST_ERR_ALREADY
;
690 spin_lock_irqsave(&st_gdata
->lock
, flags
);
691 st_gdata
->list
[new_proto
->type
] = new_proto
;
692 new_proto
->write
= st_write
;
693 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
696 /* if fw is already downloaded & new stack registers protocol */
698 switch (new_proto
->type
) {
704 st_kim_chip_toggle(new_proto
->type
, KIM_GPIO_ACTIVE
);
708 pr_err("%d protocol not supported",
710 err
= ST_ERR_NOPROTO
;
711 /* something wrong */
714 st_gdata
->list
[new_proto
->type
] = new_proto
;
715 new_proto
->write
= st_write
;
717 /* lock already held before entering else */
718 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
721 pr_info("done %s(%d) ", __func__
, new_proto
->type
);
723 EXPORT_SYMBOL_GPL(st_register
);
725 /* to unregister a protocol -
726 * to be called from protocol stack driver
728 long st_unregister(enum proto_type type
)
730 long err
= ST_SUCCESS
;
731 unsigned long flags
= 0;
732 struct st_data_s
*st_gdata
;
734 pr_info("%s: %d ", __func__
, type
);
736 st_kim_ref(&st_gdata
);
737 if (type
< ST_BT
|| type
>= ST_MAX
) {
738 pr_err(" protocol %d not supported", type
);
739 return ST_ERR_NOPROTO
;
742 spin_lock_irqsave(&st_gdata
->lock
, flags
);
744 if (st_gdata
->list
[type
] == NULL
) {
745 pr_err(" protocol %d not registered", type
);
746 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
747 return ST_ERR_NOPROTO
;
750 st_gdata
->protos_registered
--;
751 st_gdata
->list
[type
] = NULL
;
753 /* kim ignores BT in the below function
754 * and handles the rest, BT is toggled
755 * only in kim_start and kim_stop
757 st_kim_chip_toggle(type
, KIM_GPIO_INACTIVE
);
758 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
760 if ((st_gdata
->protos_registered
== ST_EMPTY
) &&
761 (!test_bit(ST_REG_PENDING
, &st_gdata
->st_state
))) {
762 pr_info(" all protocols unregistered ");
764 /* stop traffic on tty */
766 tty_ldisc_flush(st_gdata
->tty
);
767 stop_tty(st_gdata
->tty
);
770 /* all protocols now unregistered */
773 st_ll_disable(st_gdata
);
779 * called in protocol stack drivers
780 * via the write function pointer
782 long st_write(struct sk_buff
*skb
)
784 struct st_data_s
*st_gdata
;
786 enum proto_type protoid
= ST_MAX
;
790 st_kim_ref(&st_gdata
);
791 if (unlikely(skb
== NULL
|| st_gdata
== NULL
792 || st_gdata
->tty
== NULL
)) {
793 pr_err("data/tty unavailable to perform write");
794 return ST_ERR_FAILURE
;
796 #ifdef DEBUG /* open-up skb to read the 1st byte */
797 switch (skb
->data
[0]) {
798 case HCI_COMMAND_PKT
:
799 case HCI_ACLDATA_PKT
:
800 case HCI_SCODATA_PKT
:
810 if (unlikely(st_gdata
->list
[protoid
] == NULL
)) {
811 pr_err(" protocol %d not registered, and writing? ",
813 return ST_ERR_FAILURE
;
816 pr_info("%d to be written", skb
->len
);
819 /* st_ll to decide where to enqueue the skb */
820 st_int_enqueue(st_gdata
, skb
);
822 st_tx_wakeup(st_gdata
);
824 /* return number of bytes written */
828 /* for protocols making use of shared transport */
829 EXPORT_SYMBOL_GPL(st_unregister
);
831 /********************************************************************/
833 * functions called from TTY layer
835 static int st_tty_open(struct tty_struct
*tty
)
837 int err
= ST_SUCCESS
;
838 struct st_data_s
*st_gdata
;
839 pr_info("%s ", __func__
);
841 st_kim_ref(&st_gdata
);
843 tty
->disc_data
= st_gdata
;
845 /* don't do an wakeup for now */
846 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
848 /* mem already allocated
850 tty
->receive_room
= 65536;
851 /* Flush any pending characters in the driver and discipline. */
852 tty_ldisc_flush(tty
);
853 tty_driver_flush_buffer(tty
);
855 * signal to UIM via KIM that -
856 * installation of N_TI_WL ldisc is complete
859 pr_info("done %s", __func__
);
863 static void st_tty_close(struct tty_struct
*tty
)
865 unsigned char i
= ST_MAX
;
866 unsigned long flags
= 0;
867 struct st_data_s
*st_gdata
= tty
->disc_data
;
869 pr_info("%s ", __func__
);
872 * if a protocol has been registered & line discipline
873 * un-installed for some reason - what should be done ?
875 spin_lock_irqsave(&st_gdata
->lock
, flags
);
876 for (i
= ST_BT
; i
< ST_MAX
; i
++) {
877 if (st_gdata
->list
[i
] != NULL
)
878 pr_err("%d not un-registered", i
);
879 st_gdata
->list
[i
] = NULL
;
881 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
883 * signal to UIM via KIM that -
884 * N_TI_WL ldisc is un-installed
887 st_gdata
->tty
= NULL
;
888 /* Flush any pending characters in the driver and discipline. */
889 tty_ldisc_flush(tty
);
890 tty_driver_flush_buffer(tty
);
892 spin_lock_irqsave(&st_gdata
->lock
, flags
);
893 /* empty out txq and tx_waitq */
894 skb_queue_purge(&st_gdata
->txq
);
895 skb_queue_purge(&st_gdata
->tx_waitq
);
896 /* reset the TTY Rx states of ST */
897 st_gdata
->rx_count
= 0;
898 st_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
899 kfree_skb(st_gdata
->rx_skb
);
900 st_gdata
->rx_skb
= NULL
;
901 spin_unlock_irqrestore(&st_gdata
->lock
, flags
);
903 pr_info("%s: done ", __func__
);
906 static void st_tty_receive(struct tty_struct
*tty
, const unsigned char *data
,
907 char *tty_flags
, int count
)
912 printk(KERN_ERR
"incoming data...\n");
913 for (i
= 0; i
< count
; i
++)
914 printk(" %x", data
[i
]);
915 printk(KERN_ERR
"\n.. data end\n");
919 * if fw download is in progress then route incoming data
920 * to KIM for validation
922 st_recv(tty
->disc_data
, data
, count
);
923 pr_info("done %s", __func__
);
926 /* wake-up function called in from the TTY layer
927 * inside the internal wakeup function will be called
929 static void st_tty_wakeup(struct tty_struct
*tty
)
931 struct st_data_s
*st_gdata
= tty
->disc_data
;
932 pr_info("%s ", __func__
);
933 /* don't do an wakeup for now */
934 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
936 /* call our internal wakeup */
937 st_tx_wakeup((void *)st_gdata
);
940 static void st_tty_flush_buffer(struct tty_struct
*tty
)
942 struct st_data_s
*st_gdata
= tty
->disc_data
;
943 pr_info("%s ", __func__
);
945 kfree_skb(st_gdata
->tx_skb
);
946 st_gdata
->tx_skb
= NULL
;
948 tty
->ops
->flush_buffer(tty
);
952 /********************************************************************/
953 int st_core_init(struct st_data_s
**core_data
)
955 struct st_data_s
*st_gdata
;
957 static struct tty_ldisc_ops
*st_ldisc_ops
;
959 /* populate and register to TTY line discipline */
960 st_ldisc_ops
= kzalloc(sizeof(*st_ldisc_ops
), GFP_KERNEL
);
962 pr_err("no mem to allocate");
966 st_ldisc_ops
->magic
= TTY_LDISC_MAGIC
;
967 st_ldisc_ops
->name
= "n_st"; /*"n_hci"; */
968 st_ldisc_ops
->open
= st_tty_open
;
969 st_ldisc_ops
->close
= st_tty_close
;
970 st_ldisc_ops
->receive_buf
= st_tty_receive
;
971 st_ldisc_ops
->write_wakeup
= st_tty_wakeup
;
972 st_ldisc_ops
->flush_buffer
= st_tty_flush_buffer
;
973 st_ldisc_ops
->owner
= THIS_MODULE
;
975 err
= tty_register_ldisc(N_TI_WL
, st_ldisc_ops
);
977 pr_err("error registering %d line discipline %ld",
982 pr_info("registered n_shared line discipline");
984 st_gdata
= kzalloc(sizeof(struct st_data_s
), GFP_KERNEL
);
986 pr_err("memory allocation failed");
987 err
= tty_unregister_ldisc(N_TI_WL
);
989 pr_err("unable to un-register ldisc %ld", err
);
995 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
996 * will be pushed in this queue for actual transmission.
998 skb_queue_head_init(&st_gdata
->txq
);
999 skb_queue_head_init(&st_gdata
->tx_waitq
);
1001 /* Locking used in st_int_enqueue() to avoid multiple execution */
1002 spin_lock_init(&st_gdata
->lock
);
1004 /* ldisc_ops ref to be only used in __exit of module */
1005 st_gdata
->ldisc_ops
= st_ldisc_ops
;
1008 err
= st_kim_init();
1010 pr_err("error during kim initialization(%ld)", err
);
1012 err
= tty_unregister_ldisc(N_TI_WL
);
1014 pr_err("unable to un-register ldisc");
1015 kfree(st_ldisc_ops
);
1020 err
= st_ll_init(st_gdata
);
1022 pr_err("error during st_ll initialization(%ld)", err
);
1024 err
= tty_unregister_ldisc(N_TI_WL
);
1026 pr_err("unable to un-register ldisc");
1027 kfree(st_ldisc_ops
);
1030 *core_data
= st_gdata
;
1034 void st_core_exit(struct st_data_s
*st_gdata
)
1037 /* internal module cleanup */
1038 err
= st_ll_deinit(st_gdata
);
1040 pr_err("error during deinit of ST LL %ld", err
);
1042 err
= st_kim_deinit();
1044 pr_err("error during deinit of ST KIM %ld", err
);
1046 if (st_gdata
!= NULL
) {
1047 /* Free ST Tx Qs and skbs */
1048 skb_queue_purge(&st_gdata
->txq
);
1049 skb_queue_purge(&st_gdata
->tx_waitq
);
1050 kfree_skb(st_gdata
->rx_skb
);
1051 kfree_skb(st_gdata
->tx_skb
);
1052 /* TTY ldisc cleanup */
1053 err
= tty_unregister_ldisc(N_TI_WL
);
1055 pr_err("unable to un-register ldisc %ld", err
);
1056 kfree(st_gdata
->ldisc_ops
);
1057 /* free the global data pointer */