Linux 4.2.1
[linux/fpc-iii.git] / drivers / misc / ti-st / st_core.c
blobc8c6a363069cc189b799bcf376fcf6991cc05e6e
1 /*
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/tty.h>
27 #include <linux/seq_file.h>
28 #include <linux/skbuff.h>
30 #include <linux/ti_wilink_st.h>
32 extern void st_kim_recv(void *, const unsigned char *, long);
33 void st_int_recv(void *, const unsigned char *, long);
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 static void (*st_recv) (void *, const unsigned char *, long);
40 /********************************************************************/
41 static void add_channel_to_table(struct st_data_s *st_gdata,
42 struct st_proto_s *new_proto)
44 pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
45 /* list now has the channel id as index itself */
46 st_gdata->list[new_proto->chnl_id] = new_proto;
47 st_gdata->is_registered[new_proto->chnl_id] = true;
50 static void remove_channel_from_table(struct st_data_s *st_gdata,
51 struct st_proto_s *proto)
53 pr_info("%s: id %d\n", __func__, proto->chnl_id);
54 /* st_gdata->list[proto->chnl_id] = NULL; */
55 st_gdata->is_registered[proto->chnl_id] = false;
59 * called from KIM during firmware download.
61 * This is a wrapper function to tty->ops->write_room.
62 * It returns number of free space available in
63 * uart tx buffer.
65 int st_get_uart_wr_room(struct st_data_s *st_gdata)
67 struct tty_struct *tty;
68 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
69 pr_err("tty unavailable to perform write");
70 return -1;
72 tty = st_gdata->tty;
73 return tty->ops->write_room(tty);
76 /* can be called in from
77 * -- KIM (during fw download)
78 * -- ST Core (during st_write)
80 * This is the internal write function - a wrapper
81 * to tty->ops->write
83 int st_int_write(struct st_data_s *st_gdata,
84 const unsigned char *data, int count)
86 struct tty_struct *tty;
87 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
88 pr_err("tty unavailable to perform write");
89 return -EINVAL;
91 tty = st_gdata->tty;
92 #ifdef VERBOSE
93 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
94 16, 1, data, count, 0);
95 #endif
96 return tty->ops->write(tty, data, count);
101 * push the skb received to relevant
102 * protocol stacks
104 static void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
106 pr_debug(" %s(prot:%d) ", __func__, chnl_id);
108 if (unlikely
109 (st_gdata == NULL || st_gdata->rx_skb == NULL
110 || st_gdata->is_registered[chnl_id] == false)) {
111 pr_err("chnl_id %d not registered, no data to send?",
112 chnl_id);
113 kfree_skb(st_gdata->rx_skb);
114 return;
116 /* this cannot fail
117 * this shouldn't take long
118 * - should be just skb_queue_tail for the
119 * protocol stack driver
121 if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
122 if (unlikely
123 (st_gdata->list[chnl_id]->recv
124 (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
125 != 0)) {
126 pr_err(" proto stack %d's ->recv failed", chnl_id);
127 kfree_skb(st_gdata->rx_skb);
128 return;
130 } else {
131 pr_err(" proto stack %d's ->recv null", chnl_id);
132 kfree_skb(st_gdata->rx_skb);
134 return;
138 * st_reg_complete -
139 * to call registration complete callbacks
140 * of all protocol stack drivers
141 * This function is being called with spin lock held, protocol drivers are
142 * only expected to complete their waits and do nothing more than that.
144 static void st_reg_complete(struct st_data_s *st_gdata, char err)
146 unsigned char i = 0;
147 pr_info(" %s ", __func__);
148 for (i = 0; i < ST_MAX_CHANNELS; i++) {
149 if (likely(st_gdata != NULL &&
150 st_gdata->is_registered[i] == true &&
151 st_gdata->list[i]->reg_complete_cb != NULL)) {
152 st_gdata->list[i]->reg_complete_cb
153 (st_gdata->list[i]->priv_data, err);
154 pr_info("protocol %d's cb sent %d\n", i, err);
155 if (err) { /* cleanup registered protocol */
156 st_gdata->is_registered[i] = false;
157 if (st_gdata->protos_registered)
158 st_gdata->protos_registered--;
164 static inline int st_check_data_len(struct st_data_s *st_gdata,
165 unsigned char chnl_id, int len)
167 int room = skb_tailroom(st_gdata->rx_skb);
169 pr_debug("len %d room %d", len, room);
171 if (!len) {
172 /* Received packet has only packet header and
173 * has zero length payload. So, ask ST CORE to
174 * forward the packet to protocol driver (BT/FM/GPS)
176 st_send_frame(chnl_id, st_gdata);
178 } else if (len > room) {
179 /* Received packet's payload length is larger.
180 * We can't accommodate it in created skb.
182 pr_err("Data length is too large len %d room %d", len,
183 room);
184 kfree_skb(st_gdata->rx_skb);
185 } else {
186 /* Packet header has non-zero payload length and
187 * we have enough space in created skb. Lets read
188 * payload data */
189 st_gdata->rx_state = ST_W4_DATA;
190 st_gdata->rx_count = len;
191 return len;
194 /* Change ST state to continue to process next
195 * packet */
196 st_gdata->rx_state = ST_W4_PACKET_TYPE;
197 st_gdata->rx_skb = NULL;
198 st_gdata->rx_count = 0;
199 st_gdata->rx_chnl = 0;
201 return 0;
205 * st_wakeup_ack - internal function for action when wake-up ack
206 * received
208 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
209 unsigned char cmd)
211 struct sk_buff *waiting_skb;
212 unsigned long flags = 0;
214 spin_lock_irqsave(&st_gdata->lock, flags);
215 /* de-Q from waitQ and Q in txQ now that the
216 * chip is awake
218 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
219 skb_queue_tail(&st_gdata->txq, waiting_skb);
221 /* state forwarded to ST LL */
222 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
223 spin_unlock_irqrestore(&st_gdata->lock, flags);
225 /* wake up to send the recently copied skbs from waitQ */
226 st_tx_wakeup(st_gdata);
230 * st_int_recv - ST's internal receive function.
231 * Decodes received RAW data and forwards to corresponding
232 * client drivers (Bluetooth,FM,GPS..etc).
233 * This can receive various types of packets,
234 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
235 * CH-8 packets from FM, CH-9 packets from GPS cores.
237 void st_int_recv(void *disc_data,
238 const unsigned char *data, long count)
240 char *ptr;
241 struct st_proto_s *proto;
242 unsigned short payload_len = 0;
243 int len = 0;
244 unsigned char type = 0;
245 unsigned char *plen;
246 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
247 unsigned long flags;
249 ptr = (char *)data;
250 /* tty_receive sent null ? */
251 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
252 pr_err(" received null from TTY ");
253 return;
256 pr_debug("count %ld rx_state %ld"
257 "rx_count %ld", count, st_gdata->rx_state,
258 st_gdata->rx_count);
260 spin_lock_irqsave(&st_gdata->lock, flags);
261 /* Decode received bytes here */
262 while (count) {
263 if (st_gdata->rx_count) {
264 len = min_t(unsigned int, st_gdata->rx_count, count);
265 memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
266 st_gdata->rx_count -= len;
267 count -= len;
268 ptr += len;
270 if (st_gdata->rx_count)
271 continue;
273 /* Check ST RX state machine , where are we? */
274 switch (st_gdata->rx_state) {
275 /* Waiting for complete packet ? */
276 case ST_W4_DATA:
277 pr_debug("Complete pkt received");
278 /* Ask ST CORE to forward
279 * the packet to protocol driver */
280 st_send_frame(st_gdata->rx_chnl, st_gdata);
282 st_gdata->rx_state = ST_W4_PACKET_TYPE;
283 st_gdata->rx_skb = NULL;
284 continue;
285 /* parse the header to know details */
286 case ST_W4_HEADER:
287 proto = st_gdata->list[st_gdata->rx_chnl];
288 plen =
289 &st_gdata->rx_skb->data
290 [proto->offset_len_in_hdr];
291 pr_debug("plen pointing to %x\n", *plen);
292 if (proto->len_size == 1)/* 1 byte len field */
293 payload_len = *(unsigned char *)plen;
294 else if (proto->len_size == 2)
295 payload_len =
296 __le16_to_cpu(*(unsigned short *)plen);
297 else
298 pr_info("%s: invalid length "
299 "for id %d\n",
300 __func__, proto->chnl_id);
301 st_check_data_len(st_gdata, proto->chnl_id,
302 payload_len);
303 pr_debug("off %d, pay len %d\n",
304 proto->offset_len_in_hdr, payload_len);
305 continue;
306 } /* end of switch rx_state */
309 /* end of if rx_count */
310 /* Check first byte of packet and identify module
311 * owner (BT/FM/GPS) */
312 switch (*ptr) {
313 case LL_SLEEP_IND:
314 case LL_SLEEP_ACK:
315 case LL_WAKE_UP_IND:
316 pr_debug("PM packet");
317 /* this takes appropriate action based on
318 * sleep state received --
320 st_ll_sleep_state(st_gdata, *ptr);
321 /* if WAKEUP_IND collides copy from waitq to txq
322 * and assume chip awake
324 spin_unlock_irqrestore(&st_gdata->lock, flags);
325 if (st_ll_getstate(st_gdata) == ST_LL_AWAKE)
326 st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK);
327 spin_lock_irqsave(&st_gdata->lock, flags);
329 ptr++;
330 count--;
331 continue;
332 case LL_WAKE_UP_ACK:
333 pr_debug("PM packet");
335 spin_unlock_irqrestore(&st_gdata->lock, flags);
336 /* wake up ack received */
337 st_wakeup_ack(st_gdata, *ptr);
338 spin_lock_irqsave(&st_gdata->lock, flags);
340 ptr++;
341 count--;
342 continue;
343 /* Unknow packet? */
344 default:
345 type = *ptr;
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
353 * protocols only.
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);
363 goto done;
366 st_gdata->rx_skb = alloc_skb(
367 st_gdata->list[type]->max_frame_size,
368 GFP_ATOMIC);
369 if (st_gdata->rx_skb == NULL) {
370 pr_err("out of memory: dropping\n");
371 goto done;
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);
384 ptr++;
385 count--;
387 done:
388 spin_unlock_irqrestore(&st_gdata->lock, flags);
389 pr_debug("done %s", __func__);
390 return;
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
417 * wakeup the chip.
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)) {
429 case ST_LL_AWAKE:
430 pr_debug("ST LL is AWAKE, sending normally");
431 skb_queue_tail(&st_gdata->txq, skb);
432 break;
433 case ST_LL_ASLEEP_TO_AWAKE:
434 skb_queue_tail(&st_gdata->tx_waitq, skb);
435 break;
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));
439 kfree_skb(skb);
440 break;
441 case ST_LL_ASLEEP:
442 skb_queue_tail(&st_gdata->tx_waitq, skb);
443 st_ll_wakeup(st_gdata);
444 break;
445 default:
446 pr_err("ST LL is illegal state(%ld),"
447 "purging received skb.", st_ll_getstate(st_gdata));
448 kfree_skb(skb);
449 break;
452 spin_unlock_irqrestore(&st_gdata->lock, flags);
453 pr_debug("done %s", __func__);
454 return;
458 * internal wakeup function
459 * called from either
460 * - TTY layer when write's finished
461 * - st_write (in context of the protocol stack)
463 void st_tx_wakeup(struct st_data_s *st_data)
465 struct sk_buff *skb;
466 unsigned long flags; /* for irq save flags */
467 pr_debug("%s", __func__);
468 /* check for sending & set flag sending here */
469 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
470 pr_debug("ST already sending");
471 /* keep sending */
472 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
473 return;
474 /* TX_WAKEUP will be checked in another
475 * context
478 do { /* come back if st_tx_wakeup is set */
479 /* woke-up to write */
480 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
481 while ((skb = st_int_dequeue(st_data))) {
482 int len;
483 spin_lock_irqsave(&st_data->lock, flags);
484 /* enable wake-up from TTY */
485 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
486 len = st_int_write(st_data, skb->data, skb->len);
487 skb_pull(skb, len);
488 /* if skb->len = len as expected, skb->len=0 */
489 if (skb->len) {
490 /* would be the next skb to be sent */
491 st_data->tx_skb = skb;
492 spin_unlock_irqrestore(&st_data->lock, flags);
493 break;
495 kfree_skb(skb);
496 spin_unlock_irqrestore(&st_data->lock, flags);
498 /* if wake-up is set in another context- restart sending */
499 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
501 /* clear flag sending */
502 clear_bit(ST_TX_SENDING, &st_data->tx_state);
505 /********************************************************************/
506 /* functions called from ST KIM
508 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
510 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
511 st_gdata->protos_registered,
512 st_gdata->is_registered[0x04] == true ? 'R' : 'U',
513 st_gdata->is_registered[0x08] == true ? 'R' : 'U',
514 st_gdata->is_registered[0x09] == true ? 'R' : 'U');
517 /********************************************************************/
519 * functions called from protocol stack drivers
520 * to be EXPORT-ed
522 long st_register(struct st_proto_s *new_proto)
524 struct st_data_s *st_gdata;
525 long err = 0;
526 unsigned long flags = 0;
528 st_kim_ref(&st_gdata, 0);
529 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
530 || new_proto->reg_complete_cb == NULL) {
531 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
532 return -EINVAL;
535 if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
536 pr_err("chnl_id %d not supported", new_proto->chnl_id);
537 return -EPROTONOSUPPORT;
540 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
541 pr_err("chnl_id %d already registered", new_proto->chnl_id);
542 return -EALREADY;
545 /* can be from process context only */
546 spin_lock_irqsave(&st_gdata->lock, flags);
548 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
549 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
550 /* fw download in progress */
552 add_channel_to_table(st_gdata, new_proto);
553 st_gdata->protos_registered++;
554 new_proto->write = st_write;
556 set_bit(ST_REG_PENDING, &st_gdata->st_state);
557 spin_unlock_irqrestore(&st_gdata->lock, flags);
558 return -EINPROGRESS;
559 } else if (st_gdata->protos_registered == ST_EMPTY) {
560 pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
561 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
562 st_recv = st_kim_recv;
564 /* enable the ST LL - to set default chip state */
565 st_ll_enable(st_gdata);
567 /* release lock previously held - re-locked below */
568 spin_unlock_irqrestore(&st_gdata->lock, flags);
570 /* this may take a while to complete
571 * since it involves BT fw download
573 err = st_kim_start(st_gdata->kim_data);
574 if (err != 0) {
575 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
576 if ((st_gdata->protos_registered != ST_EMPTY) &&
577 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
578 pr_err(" KIM failure complete callback ");
579 spin_lock_irqsave(&st_gdata->lock, flags);
580 st_reg_complete(st_gdata, err);
581 spin_unlock_irqrestore(&st_gdata->lock, flags);
582 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
584 return -EINVAL;
587 spin_lock_irqsave(&st_gdata->lock, flags);
589 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
590 st_recv = st_int_recv;
592 /* this is where all pending registration
593 * are signalled to be complete by calling callback functions
595 if ((st_gdata->protos_registered != ST_EMPTY) &&
596 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
597 pr_debug(" call reg complete callback ");
598 st_reg_complete(st_gdata, 0);
600 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
602 /* check for already registered once more,
603 * since the above check is old
605 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
606 pr_err(" proto %d already registered ",
607 new_proto->chnl_id);
608 spin_unlock_irqrestore(&st_gdata->lock, flags);
609 return -EALREADY;
612 add_channel_to_table(st_gdata, new_proto);
613 st_gdata->protos_registered++;
614 new_proto->write = st_write;
615 spin_unlock_irqrestore(&st_gdata->lock, flags);
616 return err;
618 /* if fw is already downloaded & new stack registers protocol */
619 else {
620 add_channel_to_table(st_gdata, new_proto);
621 st_gdata->protos_registered++;
622 new_proto->write = st_write;
624 /* lock already held before entering else */
625 spin_unlock_irqrestore(&st_gdata->lock, flags);
626 return err;
628 pr_debug("done %s(%d) ", __func__, new_proto->chnl_id);
630 EXPORT_SYMBOL_GPL(st_register);
632 /* to unregister a protocol -
633 * to be called from protocol stack driver
635 long st_unregister(struct st_proto_s *proto)
637 long err = 0;
638 unsigned long flags = 0;
639 struct st_data_s *st_gdata;
641 pr_debug("%s: %d ", __func__, proto->chnl_id);
643 st_kim_ref(&st_gdata, 0);
644 if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
645 pr_err(" chnl_id %d not supported", proto->chnl_id);
646 return -EPROTONOSUPPORT;
649 spin_lock_irqsave(&st_gdata->lock, flags);
651 if (st_gdata->is_registered[proto->chnl_id] == false) {
652 pr_err(" chnl_id %d not registered", proto->chnl_id);
653 spin_unlock_irqrestore(&st_gdata->lock, flags);
654 return -EPROTONOSUPPORT;
657 if (st_gdata->protos_registered)
658 st_gdata->protos_registered--;
660 remove_channel_from_table(st_gdata, proto);
661 spin_unlock_irqrestore(&st_gdata->lock, flags);
663 if ((st_gdata->protos_registered == ST_EMPTY) &&
664 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
665 pr_info(" all chnl_ids unregistered ");
667 /* stop traffic on tty */
668 if (st_gdata->tty) {
669 tty_ldisc_flush(st_gdata->tty);
670 stop_tty(st_gdata->tty);
673 /* all chnl_ids now unregistered */
674 st_kim_stop(st_gdata->kim_data);
675 /* disable ST LL */
676 st_ll_disable(st_gdata);
678 return err;
682 * called in protocol stack drivers
683 * via the write function pointer
685 long st_write(struct sk_buff *skb)
687 struct st_data_s *st_gdata;
688 long len;
690 st_kim_ref(&st_gdata, 0);
691 if (unlikely(skb == NULL || st_gdata == NULL
692 || st_gdata->tty == NULL)) {
693 pr_err("data/tty unavailable to perform write");
694 return -EINVAL;
697 pr_debug("%d to be written", skb->len);
698 len = skb->len;
700 /* st_ll to decide where to enqueue the skb */
701 st_int_enqueue(st_gdata, skb);
702 /* wake up */
703 st_tx_wakeup(st_gdata);
705 /* return number of bytes written */
706 return len;
709 /* for protocols making use of shared transport */
710 EXPORT_SYMBOL_GPL(st_unregister);
712 /********************************************************************/
714 * functions called from TTY layer
716 static int st_tty_open(struct tty_struct *tty)
718 int err = 0;
719 struct st_data_s *st_gdata;
720 pr_info("%s ", __func__);
722 st_kim_ref(&st_gdata, 0);
723 st_gdata->tty = tty;
724 tty->disc_data = st_gdata;
726 /* don't do an wakeup for now */
727 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
729 /* mem already allocated
731 tty->receive_room = 65536;
732 /* Flush any pending characters in the driver and discipline. */
733 tty_ldisc_flush(tty);
734 tty_driver_flush_buffer(tty);
736 * signal to UIM via KIM that -
737 * installation of N_TI_WL ldisc is complete
739 st_kim_complete(st_gdata->kim_data);
740 pr_debug("done %s", __func__);
741 return err;
744 static void st_tty_close(struct tty_struct *tty)
746 unsigned char i = ST_MAX_CHANNELS;
747 unsigned long flags = 0;
748 struct st_data_s *st_gdata = tty->disc_data;
750 pr_info("%s ", __func__);
752 /* TODO:
753 * if a protocol has been registered & line discipline
754 * un-installed for some reason - what should be done ?
756 spin_lock_irqsave(&st_gdata->lock, flags);
757 for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
758 if (st_gdata->is_registered[i] == true)
759 pr_err("%d not un-registered", i);
760 st_gdata->list[i] = NULL;
761 st_gdata->is_registered[i] = false;
763 st_gdata->protos_registered = 0;
764 spin_unlock_irqrestore(&st_gdata->lock, flags);
766 * signal to UIM via KIM that -
767 * N_TI_WL ldisc is un-installed
769 st_kim_complete(st_gdata->kim_data);
770 st_gdata->tty = NULL;
771 /* Flush any pending characters in the driver and discipline. */
772 tty_ldisc_flush(tty);
773 tty_driver_flush_buffer(tty);
775 spin_lock_irqsave(&st_gdata->lock, flags);
776 /* empty out txq and tx_waitq */
777 skb_queue_purge(&st_gdata->txq);
778 skb_queue_purge(&st_gdata->tx_waitq);
779 /* reset the TTY Rx states of ST */
780 st_gdata->rx_count = 0;
781 st_gdata->rx_state = ST_W4_PACKET_TYPE;
782 kfree_skb(st_gdata->rx_skb);
783 st_gdata->rx_skb = NULL;
784 spin_unlock_irqrestore(&st_gdata->lock, flags);
786 pr_debug("%s: done ", __func__);
789 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
790 char *tty_flags, int count)
792 #ifdef VERBOSE
793 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
794 16, 1, data, count, 0);
795 #endif
798 * if fw download is in progress then route incoming data
799 * to KIM for validation
801 st_recv(tty->disc_data, data, count);
802 pr_debug("done %s", __func__);
805 /* wake-up function called in from the TTY layer
806 * inside the internal wakeup function will be called
808 static void st_tty_wakeup(struct tty_struct *tty)
810 struct st_data_s *st_gdata = tty->disc_data;
811 pr_debug("%s ", __func__);
812 /* don't do an wakeup for now */
813 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
815 /* call our internal wakeup */
816 st_tx_wakeup((void *)st_gdata);
819 static void st_tty_flush_buffer(struct tty_struct *tty)
821 struct st_data_s *st_gdata = tty->disc_data;
822 pr_debug("%s ", __func__);
824 kfree_skb(st_gdata->tx_skb);
825 st_gdata->tx_skb = NULL;
827 tty_driver_flush_buffer(tty);
828 return;
831 static struct tty_ldisc_ops st_ldisc_ops = {
832 .magic = TTY_LDISC_MAGIC,
833 .name = "n_st",
834 .open = st_tty_open,
835 .close = st_tty_close,
836 .receive_buf = st_tty_receive,
837 .write_wakeup = st_tty_wakeup,
838 .flush_buffer = st_tty_flush_buffer,
839 .owner = THIS_MODULE
842 /********************************************************************/
843 int st_core_init(struct st_data_s **core_data)
845 struct st_data_s *st_gdata;
846 long err;
848 err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops);
849 if (err) {
850 pr_err("error registering %d line discipline %ld",
851 N_TI_WL, err);
852 return err;
854 pr_debug("registered n_shared line discipline");
856 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
857 if (!st_gdata) {
858 pr_err("memory allocation failed");
859 err = tty_unregister_ldisc(N_TI_WL);
860 if (err)
861 pr_err("unable to un-register ldisc %ld", err);
862 err = -ENOMEM;
863 return err;
866 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
867 * will be pushed in this queue for actual transmission.
869 skb_queue_head_init(&st_gdata->txq);
870 skb_queue_head_init(&st_gdata->tx_waitq);
872 /* Locking used in st_int_enqueue() to avoid multiple execution */
873 spin_lock_init(&st_gdata->lock);
875 err = st_ll_init(st_gdata);
876 if (err) {
877 pr_err("error during st_ll initialization(%ld)", err);
878 kfree(st_gdata);
879 err = tty_unregister_ldisc(N_TI_WL);
880 if (err)
881 pr_err("unable to un-register ldisc");
882 return err;
884 *core_data = st_gdata;
885 return 0;
888 void st_core_exit(struct st_data_s *st_gdata)
890 long err;
891 /* internal module cleanup */
892 err = st_ll_deinit(st_gdata);
893 if (err)
894 pr_err("error during deinit of ST LL %ld", err);
896 if (st_gdata != NULL) {
897 /* Free ST Tx Qs and skbs */
898 skb_queue_purge(&st_gdata->txq);
899 skb_queue_purge(&st_gdata->tx_waitq);
900 kfree_skb(st_gdata->rx_skb);
901 kfree_skb(st_gdata->tx_skb);
902 /* TTY ldisc cleanup */
903 err = tty_unregister_ldisc(N_TI_WL);
904 if (err)
905 pr_err("unable to un-register ldisc %ld", err);
906 /* free the global data pointer */
907 kfree(st_gdata);