]> Pileus Git - ~andy/linux/blob - drivers/misc/ti-st/st_core.c
Linux 3.14
[~andy/linux] / drivers / misc / ti-st / st_core.c
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>
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
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>
27
28 #include <linux/seq_file.h>
29 #include <linux/skbuff.h>
30
31 #include <linux/ti_wilink_st.h>
32
33 extern void st_kim_recv(void *, const unsigned char *, long);
34 void st_int_recv(void *, const unsigned char *, long);
35 /* function pointer pointing to either,
36  * st_kim_recv during registration to receive fw download responses
37  * st_int_recv after registration to receive proto stack responses
38  */
39 static void (*st_recv) (void *, const unsigned char *, long);
40
41 /********************************************************************/
42 static void add_channel_to_table(struct st_data_s *st_gdata,
43                 struct st_proto_s *new_proto)
44 {
45         pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
46         /* list now has the channel id as index itself */
47         st_gdata->list[new_proto->chnl_id] = new_proto;
48         st_gdata->is_registered[new_proto->chnl_id] = true;
49 }
50
51 static void remove_channel_from_table(struct st_data_s *st_gdata,
52                 struct st_proto_s *proto)
53 {
54         pr_info("%s: id %d\n", __func__, proto->chnl_id);
55 /*      st_gdata->list[proto->chnl_id] = NULL; */
56         st_gdata->is_registered[proto->chnl_id] = false;
57 }
58
59 /*
60  * called from KIM during firmware download.
61  *
62  * This is a wrapper function to tty->ops->write_room.
63  * It returns number of free space available in
64  * uart tx buffer.
65  */
66 int st_get_uart_wr_room(struct st_data_s *st_gdata)
67 {
68         struct tty_struct *tty;
69         if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
70                 pr_err("tty unavailable to perform write");
71                 return -1;
72         }
73         tty = st_gdata->tty;
74         return tty->ops->write_room(tty);
75 }
76
77 /* can be called in from
78  * -- KIM (during fw download)
79  * -- ST Core (during st_write)
80  *
81  *  This is the internal write function - a wrapper
82  *  to tty->ops->write
83  */
84 int st_int_write(struct st_data_s *st_gdata,
85         const unsigned char *data, int count)
86 {
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 -EINVAL;
91         }
92         tty = st_gdata->tty;
93 #ifdef VERBOSE
94         print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
95                 16, 1, data, count, 0);
96 #endif
97         return tty->ops->write(tty, data, count);
98
99 }
100
101 /*
102  * push the skb received to relevant
103  * protocol stacks
104  */
105 static void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
106 {
107         pr_debug(" %s(prot:%d) ", __func__, chnl_id);
108
109         if (unlikely
110             (st_gdata == NULL || st_gdata->rx_skb == NULL
111              || st_gdata->is_registered[chnl_id] == false)) {
112                 pr_err("chnl_id %d not registered, no data to send?",
113                            chnl_id);
114                 kfree_skb(st_gdata->rx_skb);
115                 return;
116         }
117         /* this cannot fail
118          * this shouldn't take long
119          * - should be just skb_queue_tail for the
120          *   protocol stack driver
121          */
122         if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
123                 if (unlikely
124                         (st_gdata->list[chnl_id]->recv
125                         (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
126                              != 0)) {
127                         pr_err(" proto stack %d's ->recv failed", chnl_id);
128                         kfree_skb(st_gdata->rx_skb);
129                         return;
130                 }
131         } else {
132                 pr_err(" proto stack %d's ->recv null", chnl_id);
133                 kfree_skb(st_gdata->rx_skb);
134         }
135         return;
136 }
137
138 /**
139  * st_reg_complete -
140  * to call registration complete callbacks
141  * of all protocol stack drivers
142  * This function is being called with spin lock held, protocol drivers are
143  * only expected to complete their waits and do nothing more than that.
144  */
145 static void st_reg_complete(struct st_data_s *st_gdata, char err)
146 {
147         unsigned char i = 0;
148         pr_info(" %s ", __func__);
149         for (i = 0; i < ST_MAX_CHANNELS; i++) {
150                 if (likely(st_gdata != NULL &&
151                         st_gdata->is_registered[i] == true &&
152                                 st_gdata->list[i]->reg_complete_cb != NULL)) {
153                         st_gdata->list[i]->reg_complete_cb
154                                 (st_gdata->list[i]->priv_data, err);
155                         pr_info("protocol %d's cb sent %d\n", i, err);
156                         if (err) { /* cleanup registered protocol */
157                                 st_gdata->protos_registered--;
158                                 st_gdata->is_registered[i] = false;
159                         }
160                 }
161         }
162 }
163
164 static inline int st_check_data_len(struct st_data_s *st_gdata,
165         unsigned char chnl_id, int len)
166 {
167         int room = skb_tailroom(st_gdata->rx_skb);
168
169         pr_debug("len %d room %d", len, room);
170
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)
175                  */
176                 st_send_frame(chnl_id, st_gdata);
177
178         } else if (len > room) {
179                 /* Received packet's payload length is larger.
180                  * We can't accommodate it in created skb.
181                  */
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;
192         }
193
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;
200
201         return 0;
202 }
203
204 /**
205  * st_wakeup_ack - internal function for action when wake-up ack
206  *      received
207  */
208 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
209         unsigned char cmd)
210 {
211         struct sk_buff *waiting_skb;
212         unsigned long flags = 0;
213
214         spin_lock_irqsave(&st_gdata->lock, flags);
215         /* de-Q from waitQ and Q in txQ now that the
216          * chip is awake
217          */
218         while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
219                 skb_queue_tail(&st_gdata->txq, waiting_skb);
220
221         /* state forwarded to ST LL */
222         st_ll_sleep_state(st_gdata, (unsigned long)cmd);
223         spin_unlock_irqrestore(&st_gdata->lock, flags);
224
225         /* wake up to send the recently copied skbs from waitQ */
226         st_tx_wakeup(st_gdata);
227 }
228
229 /**
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.
236  */
237 void st_int_recv(void *disc_data,
238         const unsigned char *data, long count)
239 {
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;
248
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;
254         }
255
256         pr_debug("count %ld rx_state %ld"
257                    "rx_count %ld", count, st_gdata->rx_state,
258                    st_gdata->rx_count);
259
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;
269
270                         if (st_gdata->rx_count)
271                                 continue;
272
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);
281
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 */
307                 }
308
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 --
319                          */
320                         st_ll_sleep_state(st_gdata, *ptr);
321                         /* if WAKEUP_IND collides copy from waitq to txq
322                          * and assume chip awake
323                          */
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);
328
329                         ptr++;
330                         count--;
331                         continue;
332                 case LL_WAKE_UP_ACK:
333                         pr_debug("PM packet");
334
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);
339
340                         ptr++;
341                         count--;
342                         continue;
343                         /* Unknow packet? */
344                 default:
345                         type = *ptr;
346                         if (st_gdata->list[type] == NULL) {
347                                 pr_err("chip/interface misbehavior dropping"
348                                         " frame starting with 0x%02x", type);
349                                 goto done;
350
351                         }
352                         st_gdata->rx_skb = alloc_skb(
353                                         st_gdata->list[type]->max_frame_size,
354                                         GFP_ATOMIC);
355                         if (st_gdata->rx_skb == NULL) {
356                                 pr_err("out of memory: dropping\n");
357                                 goto done;
358                         }
359
360                         skb_reserve(st_gdata->rx_skb,
361                                         st_gdata->list[type]->reserve);
362                         /* next 2 required for BT only */
363                         st_gdata->rx_skb->cb[0] = type; /*pkt_type*/
364                         st_gdata->rx_skb->cb[1] = 0; /*incoming*/
365                         st_gdata->rx_chnl = *ptr;
366                         st_gdata->rx_state = ST_W4_HEADER;
367                         st_gdata->rx_count = st_gdata->list[type]->hdr_len;
368                         pr_debug("rx_count %ld\n", st_gdata->rx_count);
369                 };
370                 ptr++;
371                 count--;
372         }
373 done:
374         spin_unlock_irqrestore(&st_gdata->lock, flags);
375         pr_debug("done %s", __func__);
376         return;
377 }
378
379 /**
380  * st_int_dequeue - internal de-Q function.
381  *      If the previous data set was not written
382  *      completely, return that skb which has the pending data.
383  *      In normal cases, return top of txq.
384  */
385 static struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
386 {
387         struct sk_buff *returning_skb;
388
389         pr_debug("%s", __func__);
390         if (st_gdata->tx_skb != NULL) {
391                 returning_skb = st_gdata->tx_skb;
392                 st_gdata->tx_skb = NULL;
393                 return returning_skb;
394         }
395         return skb_dequeue(&st_gdata->txq);
396 }
397
398 /**
399  * st_int_enqueue - internal Q-ing function.
400  *      Will either Q the skb to txq or the tx_waitq
401  *      depending on the ST LL state.
402  *      If the chip is asleep, then Q it onto waitq and
403  *      wakeup the chip.
404  *      txq and waitq needs protection since the other contexts
405  *      may be sending data, waking up chip.
406  */
407 static void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
408 {
409         unsigned long flags = 0;
410
411         pr_debug("%s", __func__);
412         spin_lock_irqsave(&st_gdata->lock, flags);
413
414         switch (st_ll_getstate(st_gdata)) {
415         case ST_LL_AWAKE:
416                 pr_debug("ST LL is AWAKE, sending normally");
417                 skb_queue_tail(&st_gdata->txq, skb);
418                 break;
419         case ST_LL_ASLEEP_TO_AWAKE:
420                 skb_queue_tail(&st_gdata->tx_waitq, skb);
421                 break;
422         case ST_LL_AWAKE_TO_ASLEEP:
423                 pr_err("ST LL is illegal state(%ld),"
424                            "purging received skb.", st_ll_getstate(st_gdata));
425                 kfree_skb(skb);
426                 break;
427         case ST_LL_ASLEEP:
428                 skb_queue_tail(&st_gdata->tx_waitq, skb);
429                 st_ll_wakeup(st_gdata);
430                 break;
431         default:
432                 pr_err("ST LL is illegal state(%ld),"
433                            "purging received skb.", st_ll_getstate(st_gdata));
434                 kfree_skb(skb);
435                 break;
436         }
437
438         spin_unlock_irqrestore(&st_gdata->lock, flags);
439         pr_debug("done %s", __func__);
440         return;
441 }
442
443 /*
444  * internal wakeup function
445  * called from either
446  * - TTY layer when write's finished
447  * - st_write (in context of the protocol stack)
448  */
449 void st_tx_wakeup(struct st_data_s *st_data)
450 {
451         struct sk_buff *skb;
452         unsigned long flags;    /* for irq save flags */
453         pr_debug("%s", __func__);
454         /* check for sending & set flag sending here */
455         if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
456                 pr_debug("ST already sending");
457                 /* keep sending */
458                 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
459                 return;
460                 /* TX_WAKEUP will be checked in another
461                  * context
462                  */
463         }
464         do {                    /* come back if st_tx_wakeup is set */
465                 /* woke-up to write */
466                 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
467                 while ((skb = st_int_dequeue(st_data))) {
468                         int len;
469                         spin_lock_irqsave(&st_data->lock, flags);
470                         /* enable wake-up from TTY */
471                         set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
472                         len = st_int_write(st_data, skb->data, skb->len);
473                         skb_pull(skb, len);
474                         /* if skb->len = len as expected, skb->len=0 */
475                         if (skb->len) {
476                                 /* would be the next skb to be sent */
477                                 st_data->tx_skb = skb;
478                                 spin_unlock_irqrestore(&st_data->lock, flags);
479                                 break;
480                         }
481                         kfree_skb(skb);
482                         spin_unlock_irqrestore(&st_data->lock, flags);
483                 }
484                 /* if wake-up is set in another context- restart sending */
485         } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
486
487         /* clear flag sending */
488         clear_bit(ST_TX_SENDING, &st_data->tx_state);
489 }
490
491 /********************************************************************/
492 /* functions called from ST KIM
493 */
494 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
495 {
496         seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
497                         st_gdata->protos_registered,
498                         st_gdata->is_registered[0x04] == true ? 'R' : 'U',
499                         st_gdata->is_registered[0x08] == true ? 'R' : 'U',
500                         st_gdata->is_registered[0x09] == true ? 'R' : 'U');
501 }
502
503 /********************************************************************/
504 /*
505  * functions called from protocol stack drivers
506  * to be EXPORT-ed
507  */
508 long st_register(struct st_proto_s *new_proto)
509 {
510         struct st_data_s        *st_gdata;
511         long err = 0;
512         unsigned long flags = 0;
513
514         st_kim_ref(&st_gdata, 0);
515         if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
516             || new_proto->reg_complete_cb == NULL) {
517                 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
518                 return -EINVAL;
519         }
520
521         if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
522                 pr_err("chnl_id %d not supported", new_proto->chnl_id);
523                 return -EPROTONOSUPPORT;
524         }
525
526         if (st_gdata->is_registered[new_proto->chnl_id] == true) {
527                 pr_err("chnl_id %d already registered", new_proto->chnl_id);
528                 return -EALREADY;
529         }
530
531         /* can be from process context only */
532         spin_lock_irqsave(&st_gdata->lock, flags);
533
534         if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
535                 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
536                 /* fw download in progress */
537
538                 add_channel_to_table(st_gdata, new_proto);
539                 st_gdata->protos_registered++;
540                 new_proto->write = st_write;
541
542                 set_bit(ST_REG_PENDING, &st_gdata->st_state);
543                 spin_unlock_irqrestore(&st_gdata->lock, flags);
544                 return -EINPROGRESS;
545         } else if (st_gdata->protos_registered == ST_EMPTY) {
546                 pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
547                 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
548                 st_recv = st_kim_recv;
549
550                 /* enable the ST LL - to set default chip state */
551                 st_ll_enable(st_gdata);
552
553                 /* release lock previously held - re-locked below */
554                 spin_unlock_irqrestore(&st_gdata->lock, flags);
555
556                 /* this may take a while to complete
557                  * since it involves BT fw download
558                  */
559                 err = st_kim_start(st_gdata->kim_data);
560                 if (err != 0) {
561                         clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
562                         if ((st_gdata->protos_registered != ST_EMPTY) &&
563                             (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
564                                 pr_err(" KIM failure complete callback ");
565                                 spin_lock_irqsave(&st_gdata->lock, flags);
566                                 st_reg_complete(st_gdata, err);
567                                 spin_unlock_irqrestore(&st_gdata->lock, flags);
568                                 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
569                         }
570                         return -EINVAL;
571                 }
572
573                 spin_lock_irqsave(&st_gdata->lock, flags);
574
575                 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
576                 st_recv = st_int_recv;
577
578                 /* this is where all pending registration
579                  * are signalled to be complete by calling callback functions
580                  */
581                 if ((st_gdata->protos_registered != ST_EMPTY) &&
582                     (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
583                         pr_debug(" call reg complete callback ");
584                         st_reg_complete(st_gdata, 0);
585                 }
586                 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
587
588                 /* check for already registered once more,
589                  * since the above check is old
590                  */
591                 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
592                         pr_err(" proto %d already registered ",
593                                    new_proto->chnl_id);
594                         spin_unlock_irqrestore(&st_gdata->lock, flags);
595                         return -EALREADY;
596                 }
597
598                 add_channel_to_table(st_gdata, new_proto);
599                 st_gdata->protos_registered++;
600                 new_proto->write = st_write;
601                 spin_unlock_irqrestore(&st_gdata->lock, flags);
602                 return err;
603         }
604         /* if fw is already downloaded & new stack registers protocol */
605         else {
606                 add_channel_to_table(st_gdata, new_proto);
607                 st_gdata->protos_registered++;
608                 new_proto->write = st_write;
609
610                 /* lock already held before entering else */
611                 spin_unlock_irqrestore(&st_gdata->lock, flags);
612                 return err;
613         }
614         pr_debug("done %s(%d) ", __func__, new_proto->chnl_id);
615 }
616 EXPORT_SYMBOL_GPL(st_register);
617
618 /* to unregister a protocol -
619  * to be called from protocol stack driver
620  */
621 long st_unregister(struct st_proto_s *proto)
622 {
623         long err = 0;
624         unsigned long flags = 0;
625         struct st_data_s        *st_gdata;
626
627         pr_debug("%s: %d ", __func__, proto->chnl_id);
628
629         st_kim_ref(&st_gdata, 0);
630         if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
631                 pr_err(" chnl_id %d not supported", proto->chnl_id);
632                 return -EPROTONOSUPPORT;
633         }
634
635         spin_lock_irqsave(&st_gdata->lock, flags);
636
637         if (st_gdata->is_registered[proto->chnl_id] == false) {
638                 pr_err(" chnl_id %d not registered", proto->chnl_id);
639                 spin_unlock_irqrestore(&st_gdata->lock, flags);
640                 return -EPROTONOSUPPORT;
641         }
642
643         st_gdata->protos_registered--;
644         remove_channel_from_table(st_gdata, proto);
645         spin_unlock_irqrestore(&st_gdata->lock, flags);
646
647         /* paranoid check */
648         if (st_gdata->protos_registered < ST_EMPTY)
649                 st_gdata->protos_registered = ST_EMPTY;
650
651         if ((st_gdata->protos_registered == ST_EMPTY) &&
652             (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
653                 pr_info(" all chnl_ids unregistered ");
654
655                 /* stop traffic on tty */
656                 if (st_gdata->tty) {
657                         tty_ldisc_flush(st_gdata->tty);
658                         stop_tty(st_gdata->tty);
659                 }
660
661                 /* all chnl_ids now unregistered */
662                 st_kim_stop(st_gdata->kim_data);
663                 /* disable ST LL */
664                 st_ll_disable(st_gdata);
665         }
666         return err;
667 }
668
669 /*
670  * called in protocol stack drivers
671  * via the write function pointer
672  */
673 long st_write(struct sk_buff *skb)
674 {
675         struct st_data_s *st_gdata;
676         long len;
677
678         st_kim_ref(&st_gdata, 0);
679         if (unlikely(skb == NULL || st_gdata == NULL
680                 || st_gdata->tty == NULL)) {
681                 pr_err("data/tty unavailable to perform write");
682                 return -EINVAL;
683         }
684
685         pr_debug("%d to be written", skb->len);
686         len = skb->len;
687
688         /* st_ll to decide where to enqueue the skb */
689         st_int_enqueue(st_gdata, skb);
690         /* wake up */
691         st_tx_wakeup(st_gdata);
692
693         /* return number of bytes written */
694         return len;
695 }
696
697 /* for protocols making use of shared transport */
698 EXPORT_SYMBOL_GPL(st_unregister);
699
700 /********************************************************************/
701 /*
702  * functions called from TTY layer
703  */
704 static int st_tty_open(struct tty_struct *tty)
705 {
706         int err = 0;
707         struct st_data_s *st_gdata;
708         pr_info("%s ", __func__);
709
710         st_kim_ref(&st_gdata, 0);
711         st_gdata->tty = tty;
712         tty->disc_data = st_gdata;
713
714         /* don't do an wakeup for now */
715         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
716
717         /* mem already allocated
718          */
719         tty->receive_room = 65536;
720         /* Flush any pending characters in the driver and discipline. */
721         tty_ldisc_flush(tty);
722         tty_driver_flush_buffer(tty);
723         /*
724          * signal to UIM via KIM that -
725          * installation of N_TI_WL ldisc is complete
726          */
727         st_kim_complete(st_gdata->kim_data);
728         pr_debug("done %s", __func__);
729         return err;
730 }
731
732 static void st_tty_close(struct tty_struct *tty)
733 {
734         unsigned char i = ST_MAX_CHANNELS;
735         unsigned long flags = 0;
736         struct  st_data_s *st_gdata = tty->disc_data;
737
738         pr_info("%s ", __func__);
739
740         /* TODO:
741          * if a protocol has been registered & line discipline
742          * un-installed for some reason - what should be done ?
743          */
744         spin_lock_irqsave(&st_gdata->lock, flags);
745         for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
746                 if (st_gdata->is_registered[i] == true)
747                         pr_err("%d not un-registered", i);
748                 st_gdata->list[i] = NULL;
749                 st_gdata->is_registered[i] = false;
750         }
751         st_gdata->protos_registered = 0;
752         spin_unlock_irqrestore(&st_gdata->lock, flags);
753         /*
754          * signal to UIM via KIM that -
755          * N_TI_WL ldisc is un-installed
756          */
757         st_kim_complete(st_gdata->kim_data);
758         st_gdata->tty = NULL;
759         /* Flush any pending characters in the driver and discipline. */
760         tty_ldisc_flush(tty);
761         tty_driver_flush_buffer(tty);
762
763         spin_lock_irqsave(&st_gdata->lock, flags);
764         /* empty out txq and tx_waitq */
765         skb_queue_purge(&st_gdata->txq);
766         skb_queue_purge(&st_gdata->tx_waitq);
767         /* reset the TTY Rx states of ST */
768         st_gdata->rx_count = 0;
769         st_gdata->rx_state = ST_W4_PACKET_TYPE;
770         kfree_skb(st_gdata->rx_skb);
771         st_gdata->rx_skb = NULL;
772         spin_unlock_irqrestore(&st_gdata->lock, flags);
773
774         pr_debug("%s: done ", __func__);
775 }
776
777 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
778                            char *tty_flags, int count)
779 {
780 #ifdef VERBOSE
781         print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
782                 16, 1, data, count, 0);
783 #endif
784
785         /*
786          * if fw download is in progress then route incoming data
787          * to KIM for validation
788          */
789         st_recv(tty->disc_data, data, count);
790         pr_debug("done %s", __func__);
791 }
792
793 /* wake-up function called in from the TTY layer
794  * inside the internal wakeup function will be called
795  */
796 static void st_tty_wakeup(struct tty_struct *tty)
797 {
798         struct  st_data_s *st_gdata = tty->disc_data;
799         pr_debug("%s ", __func__);
800         /* don't do an wakeup for now */
801         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
802
803         /* call our internal wakeup */
804         st_tx_wakeup((void *)st_gdata);
805 }
806
807 static void st_tty_flush_buffer(struct tty_struct *tty)
808 {
809         struct  st_data_s *st_gdata = tty->disc_data;
810         pr_debug("%s ", __func__);
811
812         kfree_skb(st_gdata->tx_skb);
813         st_gdata->tx_skb = NULL;
814
815         tty_driver_flush_buffer(tty);
816         return;
817 }
818
819 static struct tty_ldisc_ops st_ldisc_ops = {
820         .magic = TTY_LDISC_MAGIC,
821         .name = "n_st",
822         .open = st_tty_open,
823         .close = st_tty_close,
824         .receive_buf = st_tty_receive,
825         .write_wakeup = st_tty_wakeup,
826         .flush_buffer = st_tty_flush_buffer,
827         .owner = THIS_MODULE
828 };
829
830 /********************************************************************/
831 int st_core_init(struct st_data_s **core_data)
832 {
833         struct st_data_s *st_gdata;
834         long err;
835
836         err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops);
837         if (err) {
838                 pr_err("error registering %d line discipline %ld",
839                            N_TI_WL, err);
840                 return err;
841         }
842         pr_debug("registered n_shared line discipline");
843
844         st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
845         if (!st_gdata) {
846                 pr_err("memory allocation failed");
847                 err = tty_unregister_ldisc(N_TI_WL);
848                 if (err)
849                         pr_err("unable to un-register ldisc %ld", err);
850                 err = -ENOMEM;
851                 return err;
852         }
853
854         /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
855          * will be pushed in this queue for actual transmission.
856          */
857         skb_queue_head_init(&st_gdata->txq);
858         skb_queue_head_init(&st_gdata->tx_waitq);
859
860         /* Locking used in st_int_enqueue() to avoid multiple execution */
861         spin_lock_init(&st_gdata->lock);
862
863         err = st_ll_init(st_gdata);
864         if (err) {
865                 pr_err("error during st_ll initialization(%ld)", err);
866                 kfree(st_gdata);
867                 err = tty_unregister_ldisc(N_TI_WL);
868                 if (err)
869                         pr_err("unable to un-register ldisc");
870                 return err;
871         }
872         *core_data = st_gdata;
873         return 0;
874 }
875
876 void st_core_exit(struct st_data_s *st_gdata)
877 {
878         long err;
879         /* internal module cleanup */
880         err = st_ll_deinit(st_gdata);
881         if (err)
882                 pr_err("error during deinit of ST LL %ld", err);
883
884         if (st_gdata != NULL) {
885                 /* Free ST Tx Qs and skbs */
886                 skb_queue_purge(&st_gdata->txq);
887                 skb_queue_purge(&st_gdata->tx_waitq);
888                 kfree_skb(st_gdata->rx_skb);
889                 kfree_skb(st_gdata->tx_skb);
890                 /* TTY ldisc cleanup */
891                 err = tty_unregister_ldisc(N_TI_WL);
892                 if (err)
893                         pr_err("unable to un-register ldisc %ld", err);
894                 /* free the global data pointer */
895                 kfree(st_gdata);
896         }
897 }
898
899