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