staging: ti-st: mv headers to ti_wilink_st
[cascardo/linux.git] / drivers / staging / 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 Texas Instruments
5  *
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.
9  *
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.
14  *
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
18  *
19  */
20
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>
26
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>
31 #include "ti_wilink_st.h"
32
33 /* strings to be used for rfkill entries and by
34  * ST Core to be used for sysfs debug entry
35  */
36 #define PROTO_ENTRY(type, name) name
37 const unsigned char *protocol_strngs[] = {
38         PROTO_ENTRY(ST_BT, "Bluetooth"),
39         PROTO_ENTRY(ST_FM, "FM"),
40         PROTO_ENTRY(ST_GPS, "GPS"),
41 };
42 /* function pointer pointing to either,
43  * st_kim_recv during registration to receive fw download responses
44  * st_int_recv after registration to receive proto stack responses
45  */
46 void (*st_recv) (void*, const unsigned char*, long);
47
48 /********************************************************************/
49 #if 0
50 /* internal misc functions */
51 bool is_protocol_list_empty(void)
52 {
53         unsigned char i = 0;
54         pr_debug(" %s ", __func__);
55         for (i = 0; i < ST_MAX; i++) {
56                 if (st_gdata->list[i] != NULL)
57                         return ST_NOTEMPTY;
58                 /* not empty */
59         }
60         /* list empty */
61         return ST_EMPTY;
62 }
63 #endif
64
65 /* can be called in from
66  * -- KIM (during fw download)
67  * -- ST Core (during st_write)
68  *
69  *  This is the internal write function - a wrapper
70  *  to tty->ops->write
71  */
72 int st_int_write(struct st_data_s *st_gdata,
73         const unsigned char *data, int count)
74 {
75         struct tty_struct *tty;
76         if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
77                 pr_err("tty unavailable to perform write");
78                 return -1;
79         }
80         tty = st_gdata->tty;
81 #ifdef VERBOSE
82         print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
83                 16, 1, data, count, 0);
84 #endif
85         return tty->ops->write(tty, data, count);
86
87 }
88
89 /*
90  * push the skb received to relevant
91  * protocol stacks
92  */
93 void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
94 {
95         pr_info(" %s(prot:%d) ", __func__, protoid);
96
97         if (unlikely
98             (st_gdata == NULL || st_gdata->rx_skb == NULL
99              || st_gdata->list[protoid] == NULL)) {
100                 pr_err("protocol %d not registered, no data to send?",
101                            protoid);
102                 kfree_skb(st_gdata->rx_skb);
103                 return;
104         }
105         /* this cannot fail
106          * this shouldn't take long
107          * - should be just skb_queue_tail for the
108          *   protocol stack driver
109          */
110         if (likely(st_gdata->list[protoid]->recv != NULL)) {
111                 if (unlikely
112                         (st_gdata->list[protoid]->recv
113                         (st_gdata->list[protoid]->priv_data, st_gdata->rx_skb)
114                              != 0)) {
115                         pr_err(" proto stack %d's ->recv failed", protoid);
116                         kfree_skb(st_gdata->rx_skb);
117                         return;
118                 }
119         } else {
120                 pr_err(" proto stack %d's ->recv null", protoid);
121                 kfree_skb(st_gdata->rx_skb);
122         }
123         return;
124 }
125
126 /**
127  * st_reg_complete -
128  * to call registration complete callbacks
129  * of all protocol stack drivers
130  */
131 void st_reg_complete(struct st_data_s *st_gdata, char err)
132 {
133         unsigned char i = 0;
134         pr_info(" %s ", __func__);
135         for (i = 0; i < ST_MAX; i++) {
136                 if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
137                            st_gdata->list[i]->reg_complete_cb != NULL))
138                         st_gdata->list[i]->reg_complete_cb
139                                 (st_gdata->list[i]->priv_data, err);
140         }
141 }
142
143 static inline int st_check_data_len(struct st_data_s *st_gdata,
144         int protoid, int len)
145 {
146         register int room = skb_tailroom(st_gdata->rx_skb);
147
148         pr_debug("len %d room %d", len, room);
149
150         if (!len) {
151                 /* Received packet has only packet header and
152                  * has zero length payload. So, ask ST CORE to
153                  * forward the packet to protocol driver (BT/FM/GPS)
154                  */
155                 st_send_frame(protoid, st_gdata);
156
157         } else if (len > room) {
158                 /* Received packet's payload length is larger.
159                  * We can't accommodate it in created skb.
160                  */
161                 pr_err("Data length is too large len %d room %d", len,
162                            room);
163                 kfree_skb(st_gdata->rx_skb);
164         } else {
165                 /* Packet header has non-zero payload length and
166                  * we have enough space in created skb. Lets read
167                  * payload data */
168                 st_gdata->rx_state = ST_BT_W4_DATA;
169                 st_gdata->rx_count = len;
170                 return len;
171         }
172
173         /* Change ST state to continue to process next
174          * packet */
175         st_gdata->rx_state = ST_W4_PACKET_TYPE;
176         st_gdata->rx_skb = NULL;
177         st_gdata->rx_count = 0;
178
179         return 0;
180 }
181
182 /**
183  * st_wakeup_ack - internal function for action when wake-up ack
184  *      received
185  */
186 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
187         unsigned char cmd)
188 {
189         register struct sk_buff *waiting_skb;
190         unsigned long flags = 0;
191
192         spin_lock_irqsave(&st_gdata->lock, flags);
193         /* de-Q from waitQ and Q in txQ now that the
194          * chip is awake
195          */
196         while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
197                 skb_queue_tail(&st_gdata->txq, waiting_skb);
198
199         /* state forwarded to ST LL */
200         st_ll_sleep_state(st_gdata, (unsigned long)cmd);
201         spin_unlock_irqrestore(&st_gdata->lock, flags);
202
203         /* wake up to send the recently copied skbs from waitQ */
204         st_tx_wakeup(st_gdata);
205 }
206
207 /**
208  * st_int_recv - ST's internal receive function.
209  *      Decodes received RAW data and forwards to corresponding
210  *      client drivers (Bluetooth,FM,GPS..etc).
211  *      This can receive various types of packets,
212  *      HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
213  *      CH-8 packets from FM, CH-9 packets from GPS cores.
214  */
215 void st_int_recv(void *disc_data,
216         const unsigned char *data, long count)
217 {
218         register char *ptr;
219         struct hci_event_hdr *eh;
220         struct hci_acl_hdr *ah;
221         struct hci_sco_hdr *sh;
222         struct fm_event_hdr *fm;
223         struct gps_event_hdr *gps;
224         register int len = 0, type = 0, dlen = 0;
225         static enum proto_type protoid = ST_MAX;
226         struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
227
228         ptr = (char *)data;
229         /* tty_receive sent null ? */
230         if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
231                 pr_err(" received null from TTY ");
232                 return;
233         }
234
235         pr_info("count %ld rx_state %ld"
236                    "rx_count %ld", count, st_gdata->rx_state,
237                    st_gdata->rx_count);
238
239         /* Decode received bytes here */
240         while (count) {
241                 if (st_gdata->rx_count) {
242                         len = min_t(unsigned int, st_gdata->rx_count, count);
243                         memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
244                         st_gdata->rx_count -= len;
245                         count -= len;
246                         ptr += len;
247
248                         if (st_gdata->rx_count)
249                                 continue;
250
251                         /* Check ST RX state machine , where are we? */
252                         switch (st_gdata->rx_state) {
253
254                                 /* Waiting for complete packet ? */
255                         case ST_BT_W4_DATA:
256                                 pr_debug("Complete pkt received");
257
258                                 /* Ask ST CORE to forward
259                                  * the packet to protocol driver */
260                                 st_send_frame(protoid, st_gdata);
261
262                                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
263                                 st_gdata->rx_skb = NULL;
264                                 protoid = ST_MAX;       /* is this required ? */
265                                 continue;
266
267                                 /* Waiting for Bluetooth event header ? */
268                         case ST_BT_W4_EVENT_HDR:
269                                 eh = (struct hci_event_hdr *)st_gdata->rx_skb->
270                                     data;
271
272                                 pr_debug("Event header: evt 0x%2.2x"
273                                            "plen %d", eh->evt, eh->plen);
274
275                                 st_check_data_len(st_gdata, protoid, eh->plen);
276                                 continue;
277
278                                 /* Waiting for Bluetooth acl header ? */
279                         case ST_BT_W4_ACL_HDR:
280                                 ah = (struct hci_acl_hdr *)st_gdata->rx_skb->
281                                     data;
282                                 dlen = __le16_to_cpu(ah->dlen);
283
284                                 pr_info("ACL header: dlen %d", dlen);
285
286                                 st_check_data_len(st_gdata, protoid, dlen);
287                                 continue;
288
289                                 /* Waiting for Bluetooth sco header ? */
290                         case ST_BT_W4_SCO_HDR:
291                                 sh = (struct hci_sco_hdr *)st_gdata->rx_skb->
292                                     data;
293
294                                 pr_info("SCO header: dlen %d", sh->dlen);
295
296                                 st_check_data_len(st_gdata, protoid, sh->dlen);
297                                 continue;
298                         case ST_FM_W4_EVENT_HDR:
299                                 fm = (struct fm_event_hdr *)st_gdata->rx_skb->
300                                     data;
301                                 pr_info("FM Header: ");
302                                 st_check_data_len(st_gdata, ST_FM, fm->plen);
303                                 continue;
304                                 /* TODO : Add GPS packet machine logic here */
305                         case ST_GPS_W4_EVENT_HDR:
306                                 /* [0x09 pkt hdr][R/W byte][2 byte len] */
307                                 gps = (struct gps_event_hdr *)st_gdata->rx_skb->
308                                      data;
309                                 pr_info("GPS Header: ");
310                                 st_check_data_len(st_gdata, ST_GPS, gps->plen);
311                                 continue;
312                         }       /* end of switch rx_state */
313                 }
314
315                 /* end of if rx_count */
316                 /* Check first byte of packet and identify module
317                  * owner (BT/FM/GPS) */
318                 switch (*ptr) {
319
320                         /* Bluetooth event packet? */
321                 case HCI_EVENT_PKT:
322                         pr_info("Event packet");
323                         st_gdata->rx_state = ST_BT_W4_EVENT_HDR;
324                         st_gdata->rx_count = HCI_EVENT_HDR_SIZE;
325                         type = HCI_EVENT_PKT;
326                         protoid = ST_BT;
327                         break;
328
329                         /* Bluetooth acl packet? */
330                 case HCI_ACLDATA_PKT:
331                         pr_info("ACL packet");
332                         st_gdata->rx_state = ST_BT_W4_ACL_HDR;
333                         st_gdata->rx_count = HCI_ACL_HDR_SIZE;
334                         type = HCI_ACLDATA_PKT;
335                         protoid = ST_BT;
336                         break;
337
338                         /* Bluetooth sco packet? */
339                 case HCI_SCODATA_PKT:
340                         pr_info("SCO packet");
341                         st_gdata->rx_state = ST_BT_W4_SCO_HDR;
342                         st_gdata->rx_count = HCI_SCO_HDR_SIZE;
343                         type = HCI_SCODATA_PKT;
344                         protoid = ST_BT;
345                         break;
346
347                         /* Channel 8(FM) packet? */
348                 case ST_FM_CH8_PKT:
349                         pr_info("FM CH8 packet");
350                         type = ST_FM_CH8_PKT;
351                         st_gdata->rx_state = ST_FM_W4_EVENT_HDR;
352                         st_gdata->rx_count = FM_EVENT_HDR_SIZE;
353                         protoid = ST_FM;
354                         break;
355
356                         /* Channel 9(GPS) packet? */
357                 case 0x9:       /*ST_LL_GPS_CH9_PKT */
358                         pr_info("GPS CH9 packet");
359                         type = 0x9;     /* ST_LL_GPS_CH9_PKT; */
360                         protoid = ST_GPS;
361                         st_gdata->rx_state = ST_GPS_W4_EVENT_HDR;
362                         st_gdata->rx_count = 3; /* GPS_EVENT_HDR_SIZE -1*/
363                         break;
364                 case LL_SLEEP_IND:
365                 case LL_SLEEP_ACK:
366                 case LL_WAKE_UP_IND:
367                         pr_info("PM packet");
368                         /* this takes appropriate action based on
369                          * sleep state received --
370                          */
371                         st_ll_sleep_state(st_gdata, *ptr);
372                         ptr++;
373                         count--;
374                         continue;
375                 case LL_WAKE_UP_ACK:
376                         pr_info("PM packet");
377                         /* wake up ack received */
378                         st_wakeup_ack(st_gdata, *ptr);
379                         ptr++;
380                         count--;
381                         continue;
382                         /* Unknow packet? */
383                 default:
384                         pr_err("Unknown packet type %2.2x", (__u8) *ptr);
385                         ptr++;
386                         count--;
387                         continue;
388                 };
389                 ptr++;
390                 count--;
391
392                 switch (protoid) {
393                 case ST_BT:
394                         /* Allocate new packet to hold received data */
395                         st_gdata->rx_skb =
396                             bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
397                         if (!st_gdata->rx_skb) {
398                                 pr_err("Can't allocate mem for new packet");
399                                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
400                                 st_gdata->rx_count = 0;
401                                 return;
402                         }
403                         bt_cb(st_gdata->rx_skb)->pkt_type = type;
404                         break;
405                 case ST_FM:     /* for FM */
406                         st_gdata->rx_skb =
407                             alloc_skb(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
408                         if (!st_gdata->rx_skb) {
409                                 pr_err("Can't allocate mem for new packet");
410                                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
411                                 st_gdata->rx_count = 0;
412                                 return;
413                         }
414                         /* place holder 0x08 */
415                         skb_reserve(st_gdata->rx_skb, 1);
416                         st_gdata->rx_skb->cb[0] = ST_FM_CH8_PKT;
417                         break;
418                 case ST_GPS:
419                         /* for GPS */
420                         st_gdata->rx_skb =
421                             alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC);
422                         if (!st_gdata->rx_skb) {
423                                 pr_err("Can't allocate mem for new packet");
424                                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
425                                 st_gdata->rx_count = 0;
426                                 return;
427                         }
428                         /* place holder 0x09 */
429                         skb_reserve(st_gdata->rx_skb, 1);
430                         st_gdata->rx_skb->cb[0] = 0x09; /*ST_GPS_CH9_PKT; */
431                         break;
432                 case ST_MAX:
433                         break;
434                 }
435         }
436         pr_debug("done %s", __func__);
437         return;
438 }
439
440 /**
441  * st_int_dequeue - internal de-Q function.
442  *      If the previous data set was not written
443  *      completely, return that skb which has the pending data.
444  *      In normal cases, return top of txq.
445  */
446 struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
447 {
448         struct sk_buff *returning_skb;
449
450         pr_debug("%s", __func__);
451         if (st_gdata->tx_skb != NULL) {
452                 returning_skb = st_gdata->tx_skb;
453                 st_gdata->tx_skb = NULL;
454                 return returning_skb;
455         }
456         return skb_dequeue(&st_gdata->txq);
457 }
458
459 /**
460  * st_int_enqueue - internal Q-ing function.
461  *      Will either Q the skb to txq or the tx_waitq
462  *      depending on the ST LL state.
463  *      If the chip is asleep, then Q it onto waitq and
464  *      wakeup the chip.
465  *      txq and waitq needs protection since the other contexts
466  *      may be sending data, waking up chip.
467  */
468 void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
469 {
470         unsigned long flags = 0;
471
472         pr_debug("%s", __func__);
473         spin_lock_irqsave(&st_gdata->lock, flags);
474
475         switch (st_ll_getstate(st_gdata)) {
476         case ST_LL_AWAKE:
477                 pr_info("ST LL is AWAKE, sending normally");
478                 skb_queue_tail(&st_gdata->txq, skb);
479                 break;
480         case ST_LL_ASLEEP_TO_AWAKE:
481                 skb_queue_tail(&st_gdata->tx_waitq, skb);
482                 break;
483         case ST_LL_AWAKE_TO_ASLEEP:
484                 pr_err("ST LL is illegal state(%ld),"
485                            "purging received skb.", st_ll_getstate(st_gdata));
486                 kfree_skb(skb);
487                 break;
488         case ST_LL_ASLEEP:
489                 skb_queue_tail(&st_gdata->tx_waitq, skb);
490                 st_ll_wakeup(st_gdata);
491                 break;
492         default:
493                 pr_err("ST LL is illegal state(%ld),"
494                            "purging received skb.", st_ll_getstate(st_gdata));
495                 kfree_skb(skb);
496                 break;
497         }
498
499         spin_unlock_irqrestore(&st_gdata->lock, flags);
500         pr_debug("done %s", __func__);
501         return;
502 }
503
504 /*
505  * internal wakeup function
506  * called from either
507  * - TTY layer when write's finished
508  * - st_write (in context of the protocol stack)
509  */
510 void st_tx_wakeup(struct st_data_s *st_data)
511 {
512         struct sk_buff *skb;
513         unsigned long flags;    /* for irq save flags */
514         pr_debug("%s", __func__);
515         /* check for sending & set flag sending here */
516         if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
517                 pr_info("ST already sending");
518                 /* keep sending */
519                 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
520                 return;
521                 /* TX_WAKEUP will be checked in another
522                  * context
523                  */
524         }
525         do {                    /* come back if st_tx_wakeup is set */
526                 /* woke-up to write */
527                 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
528                 while ((skb = st_int_dequeue(st_data))) {
529                         int len;
530                         spin_lock_irqsave(&st_data->lock, flags);
531                         /* enable wake-up from TTY */
532                         set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
533                         len = st_int_write(st_data, skb->data, skb->len);
534                         skb_pull(skb, len);
535                         /* if skb->len = len as expected, skb->len=0 */
536                         if (skb->len) {
537                                 /* would be the next skb to be sent */
538                                 st_data->tx_skb = skb;
539                                 spin_unlock_irqrestore(&st_data->lock, flags);
540                                 break;
541                         }
542                         kfree_skb(skb);
543                         spin_unlock_irqrestore(&st_data->lock, flags);
544                 }
545                 /* if wake-up is set in another context- restart sending */
546         } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
547
548         /* clear flag sending */
549         clear_bit(ST_TX_SENDING, &st_data->tx_state);
550 }
551
552 /********************************************************************/
553 /* functions called from ST KIM
554 */
555 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
556 {
557         seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
558                         st_gdata->protos_registered,
559                         st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
560                         st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
561                         st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
562 }
563
564 /********************************************************************/
565 /*
566  * functions called from protocol stack drivers
567  * to be EXPORT-ed
568  */
569 long st_register(struct st_proto_s *new_proto)
570 {
571         struct st_data_s        *st_gdata;
572         long err = 0;
573         unsigned long flags = 0;
574
575         st_kim_ref(&st_gdata, 0);
576         pr_info("%s(%d) ", __func__, new_proto->type);
577         if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
578             || new_proto->reg_complete_cb == NULL) {
579                 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
580                 return -1;
581         }
582
583         if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
584                 pr_err("protocol %d not supported", new_proto->type);
585                 return -EPROTONOSUPPORT;
586         }
587
588         if (st_gdata->list[new_proto->type] != NULL) {
589                 pr_err("protocol %d already registered", new_proto->type);
590                 return -EALREADY;
591         }
592
593         /* can be from process context only */
594         spin_lock_irqsave(&st_gdata->lock, flags);
595
596         if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
597                 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
598                 /* fw download in progress */
599                 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
600
601                 st_gdata->list[new_proto->type] = new_proto;
602                 st_gdata->protos_registered++;
603                 new_proto->write = st_write;
604
605                 set_bit(ST_REG_PENDING, &st_gdata->st_state);
606                 spin_unlock_irqrestore(&st_gdata->lock, flags);
607                 return -EINPROGRESS;
608         } else if (st_gdata->protos_registered == ST_EMPTY) {
609                 pr_info(" protocol list empty :%d ", new_proto->type);
610                 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
611                 st_recv = st_kim_recv;
612
613                 /* release lock previously held - re-locked below */
614                 spin_unlock_irqrestore(&st_gdata->lock, flags);
615
616                 /* enable the ST LL - to set default chip state */
617                 st_ll_enable(st_gdata);
618                 /* this may take a while to complete
619                  * since it involves BT fw download
620                  */
621                 err = st_kim_start(st_gdata->kim_data);
622                 if (err != 0) {
623                         clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
624                         if ((st_gdata->protos_registered != ST_EMPTY) &&
625                             (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
626                                 pr_err(" KIM failure complete callback ");
627                                 st_reg_complete(st_gdata, -1);
628                         }
629
630                         return -1;
631                 }
632
633                 /* the protocol might require other gpios to be toggled
634                  */
635                 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
636
637                 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
638                 st_recv = st_int_recv;
639
640                 /* this is where all pending registration
641                  * are signalled to be complete by calling callback functions
642                  */
643                 if ((st_gdata->protos_registered != ST_EMPTY) &&
644                     (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
645                         pr_debug(" call reg complete callback ");
646                         st_reg_complete(st_gdata, 0);
647                 }
648                 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
649
650                 /* check for already registered once more,
651                  * since the above check is old
652                  */
653                 if (st_gdata->list[new_proto->type] != NULL) {
654                         pr_err(" proto %d already registered ",
655                                    new_proto->type);
656                         return -EALREADY;
657                 }
658
659                 spin_lock_irqsave(&st_gdata->lock, flags);
660                 st_gdata->list[new_proto->type] = new_proto;
661                 st_gdata->protos_registered++;
662                 new_proto->write = st_write;
663                 spin_unlock_irqrestore(&st_gdata->lock, flags);
664                 return err;
665         }
666         /* if fw is already downloaded & new stack registers protocol */
667         else {
668                 switch (new_proto->type) {
669                 case ST_BT:
670                         /* do nothing */
671                         break;
672                 case ST_FM:
673                 case ST_GPS:
674                         st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
675                         break;
676                 case ST_MAX:
677                 default:
678                         pr_err("%d protocol not supported",
679                                    new_proto->type);
680                         spin_unlock_irqrestore(&st_gdata->lock, flags);
681                         return -EPROTONOSUPPORT;
682                 }
683                 st_gdata->list[new_proto->type] = new_proto;
684                 st_gdata->protos_registered++;
685                 new_proto->write = st_write;
686
687                 /* lock already held before entering else */
688                 spin_unlock_irqrestore(&st_gdata->lock, flags);
689                 return err;
690         }
691         pr_debug("done %s(%d) ", __func__, new_proto->type);
692 }
693 EXPORT_SYMBOL_GPL(st_register);
694
695 /* to unregister a protocol -
696  * to be called from protocol stack driver
697  */
698 long st_unregister(enum proto_type type)
699 {
700         long err = 0;
701         unsigned long flags = 0;
702         struct st_data_s        *st_gdata;
703
704         pr_debug("%s: %d ", __func__, type);
705
706         st_kim_ref(&st_gdata, 0);
707         if (type < ST_BT || type >= ST_MAX) {
708                 pr_err(" protocol %d not supported", type);
709                 return -EPROTONOSUPPORT;
710         }
711
712         spin_lock_irqsave(&st_gdata->lock, flags);
713
714         if (st_gdata->list[type] == NULL) {
715                 pr_err(" protocol %d not registered", type);
716                 spin_unlock_irqrestore(&st_gdata->lock, flags);
717                 return -EPROTONOSUPPORT;
718         }
719
720         st_gdata->protos_registered--;
721         st_gdata->list[type] = NULL;
722
723         /* kim ignores BT in the below function
724          * and handles the rest, BT is toggled
725          * only in kim_start and kim_stop
726          */
727         st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
728         spin_unlock_irqrestore(&st_gdata->lock, flags);
729
730         if ((st_gdata->protos_registered == ST_EMPTY) &&
731             (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
732                 pr_info(" all protocols unregistered ");
733
734                 /* stop traffic on tty */
735                 if (st_gdata->tty) {
736                         tty_ldisc_flush(st_gdata->tty);
737                         stop_tty(st_gdata->tty);
738                 }
739
740                 /* all protocols now unregistered */
741                 st_kim_stop(st_gdata->kim_data);
742                 /* disable ST LL */
743                 st_ll_disable(st_gdata);
744         }
745         return err;
746 }
747
748 /*
749  * called in protocol stack drivers
750  * via the write function pointer
751  */
752 long st_write(struct sk_buff *skb)
753 {
754         struct st_data_s *st_gdata;
755 #ifdef DEBUG
756         enum proto_type protoid = ST_MAX;
757 #endif
758         long len;
759
760         st_kim_ref(&st_gdata, 0);
761         if (unlikely(skb == NULL || st_gdata == NULL
762                 || st_gdata->tty == NULL)) {
763                 pr_err("data/tty unavailable to perform write");
764                 return -1;
765         }
766 #ifdef DEBUG                    /* open-up skb to read the 1st byte */
767         switch (skb->data[0]) {
768         case HCI_COMMAND_PKT:
769         case HCI_ACLDATA_PKT:
770         case HCI_SCODATA_PKT:
771                 protoid = ST_BT;
772                 break;
773         case ST_FM_CH8_PKT:
774                 protoid = ST_FM;
775                 break;
776         case 0x09:
777                 protoid = ST_GPS;
778                 break;
779         }
780         if (unlikely(st_gdata->list[protoid] == NULL)) {
781                 pr_err(" protocol %d not registered, and writing? ",
782                            protoid);
783                 return -1;
784         }
785 #endif
786         pr_debug("%d to be written", skb->len);
787         len = skb->len;
788
789         /* st_ll to decide where to enqueue the skb */
790         st_int_enqueue(st_gdata, skb);
791         /* wake up */
792         st_tx_wakeup(st_gdata);
793
794         /* return number of bytes written */
795         return len;
796 }
797
798 /* for protocols making use of shared transport */
799 EXPORT_SYMBOL_GPL(st_unregister);
800
801 /********************************************************************/
802 /*
803  * functions called from TTY layer
804  */
805 static int st_tty_open(struct tty_struct *tty)
806 {
807         int err = 0;
808         struct st_data_s *st_gdata;
809         pr_info("%s ", __func__);
810
811         st_kim_ref(&st_gdata, 0);
812         st_gdata->tty = tty;
813         tty->disc_data = st_gdata;
814
815         /* don't do an wakeup for now */
816         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
817
818         /* mem already allocated
819          */
820         tty->receive_room = 65536;
821         /* Flush any pending characters in the driver and discipline. */
822         tty_ldisc_flush(tty);
823         tty_driver_flush_buffer(tty);
824         /*
825          * signal to UIM via KIM that -
826          * installation of N_TI_WL ldisc is complete
827          */
828         st_kim_complete(st_gdata->kim_data);
829         pr_debug("done %s", __func__);
830         return err;
831 }
832
833 static void st_tty_close(struct tty_struct *tty)
834 {
835         unsigned char i = ST_MAX;
836         unsigned long flags = 0;
837         struct  st_data_s *st_gdata = tty->disc_data;
838
839         pr_info("%s ", __func__);
840
841         /* TODO:
842          * if a protocol has been registered & line discipline
843          * un-installed for some reason - what should be done ?
844          */
845         spin_lock_irqsave(&st_gdata->lock, flags);
846         for (i = ST_BT; i < ST_MAX; i++) {
847                 if (st_gdata->list[i] != NULL)
848                         pr_err("%d not un-registered", i);
849                 st_gdata->list[i] = NULL;
850         }
851         st_gdata->protos_registered = 0;
852         spin_unlock_irqrestore(&st_gdata->lock, flags);
853         /*
854          * signal to UIM via KIM that -
855          * N_TI_WL ldisc is un-installed
856          */
857         st_kim_complete(st_gdata->kim_data);
858         st_gdata->tty = NULL;
859         /* Flush any pending characters in the driver and discipline. */
860         tty_ldisc_flush(tty);
861         tty_driver_flush_buffer(tty);
862
863         spin_lock_irqsave(&st_gdata->lock, flags);
864         /* empty out txq and tx_waitq */
865         skb_queue_purge(&st_gdata->txq);
866         skb_queue_purge(&st_gdata->tx_waitq);
867         /* reset the TTY Rx states of ST */
868         st_gdata->rx_count = 0;
869         st_gdata->rx_state = ST_W4_PACKET_TYPE;
870         kfree_skb(st_gdata->rx_skb);
871         st_gdata->rx_skb = NULL;
872         spin_unlock_irqrestore(&st_gdata->lock, flags);
873
874         pr_debug("%s: done ", __func__);
875 }
876
877 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
878                            char *tty_flags, int count)
879 {
880
881 #ifdef VERBOSE
882         print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
883                 16, 1, data, count, 0);
884 #endif
885
886         /*
887          * if fw download is in progress then route incoming data
888          * to KIM for validation
889          */
890         st_recv(tty->disc_data, data, count);
891         pr_debug("done %s", __func__);
892 }
893
894 /* wake-up function called in from the TTY layer
895  * inside the internal wakeup function will be called
896  */
897 static void st_tty_wakeup(struct tty_struct *tty)
898 {
899         struct  st_data_s *st_gdata = tty->disc_data;
900         pr_debug("%s ", __func__);
901         /* don't do an wakeup for now */
902         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
903
904         /* call our internal wakeup */
905         st_tx_wakeup((void *)st_gdata);
906 }
907
908 static void st_tty_flush_buffer(struct tty_struct *tty)
909 {
910         struct  st_data_s *st_gdata = tty->disc_data;
911         pr_debug("%s ", __func__);
912
913         kfree_skb(st_gdata->tx_skb);
914         st_gdata->tx_skb = NULL;
915
916         tty->ops->flush_buffer(tty);
917         return;
918 }
919
920 /********************************************************************/
921 int st_core_init(struct st_data_s **core_data)
922 {
923         struct st_data_s *st_gdata;
924         long err;
925         static struct tty_ldisc_ops *st_ldisc_ops;
926
927         /* populate and register to TTY line discipline */
928         st_ldisc_ops = kzalloc(sizeof(*st_ldisc_ops), GFP_KERNEL);
929         if (!st_ldisc_ops) {
930                 pr_err("no mem to allocate");
931                 return -ENOMEM;
932         }
933
934         st_ldisc_ops->magic = TTY_LDISC_MAGIC;
935         st_ldisc_ops->name = "n_st";    /*"n_hci"; */
936         st_ldisc_ops->open = st_tty_open;
937         st_ldisc_ops->close = st_tty_close;
938         st_ldisc_ops->receive_buf = st_tty_receive;
939         st_ldisc_ops->write_wakeup = st_tty_wakeup;
940         st_ldisc_ops->flush_buffer = st_tty_flush_buffer;
941         st_ldisc_ops->owner = THIS_MODULE;
942
943         err = tty_register_ldisc(N_TI_WL, st_ldisc_ops);
944         if (err) {
945                 pr_err("error registering %d line discipline %ld",
946                            N_TI_WL, err);
947                 kfree(st_ldisc_ops);
948                 return err;
949         }
950         pr_debug("registered n_shared line discipline");
951
952         st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
953         if (!st_gdata) {
954                 pr_err("memory allocation failed");
955                 err = tty_unregister_ldisc(N_TI_WL);
956                 if (err)
957                         pr_err("unable to un-register ldisc %ld", err);
958                 kfree(st_ldisc_ops);
959                 err = -ENOMEM;
960                 return err;
961         }
962
963         /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
964          * will be pushed in this queue for actual transmission.
965          */
966         skb_queue_head_init(&st_gdata->txq);
967         skb_queue_head_init(&st_gdata->tx_waitq);
968
969         /* Locking used in st_int_enqueue() to avoid multiple execution */
970         spin_lock_init(&st_gdata->lock);
971
972         /* ldisc_ops ref to be only used in __exit of module */
973         st_gdata->ldisc_ops = st_ldisc_ops;
974
975 #if 0
976         err = st_kim_init();
977         if (err) {
978                 pr_err("error during kim initialization(%ld)", err);
979                 kfree(st_gdata);
980                 err = tty_unregister_ldisc(N_TI_WL);
981                 if (err)
982                         pr_err("unable to un-register ldisc");
983                 kfree(st_ldisc_ops);
984                 return -1;
985         }
986 #endif
987
988         err = st_ll_init(st_gdata);
989         if (err) {
990                 pr_err("error during st_ll initialization(%ld)", err);
991                 kfree(st_gdata);
992                 err = tty_unregister_ldisc(N_TI_WL);
993                 if (err)
994                         pr_err("unable to un-register ldisc");
995                 kfree(st_ldisc_ops);
996                 return -1;
997         }
998         *core_data = st_gdata;
999         return 0;
1000 }
1001
1002 void st_core_exit(struct st_data_s *st_gdata)
1003 {
1004         long err;
1005         /* internal module cleanup */
1006         err = st_ll_deinit(st_gdata);
1007         if (err)
1008                 pr_err("error during deinit of ST LL %ld", err);
1009 #if 0
1010         err = st_kim_deinit();
1011         if (err)
1012                 pr_err("error during deinit of ST KIM %ld", err);
1013 #endif
1014         if (st_gdata != NULL) {
1015                 /* Free ST Tx Qs and skbs */
1016                 skb_queue_purge(&st_gdata->txq);
1017                 skb_queue_purge(&st_gdata->tx_waitq);
1018                 kfree_skb(st_gdata->rx_skb);
1019                 kfree_skb(st_gdata->tx_skb);
1020                 /* TTY ldisc cleanup */
1021                 err = tty_unregister_ldisc(N_TI_WL);
1022                 if (err)
1023                         pr_err("unable to un-register ldisc %ld", err);
1024                 kfree(st_gdata->ldisc_ops);
1025                 /* free the global data pointer */
1026                 kfree(st_gdata);
1027         }
1028 }
1029
1030