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