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