]> Pileus Git - ~andy/linux/blob - drivers/staging/ti-st/st_core.c
b20ab730539a51f900b917d0366afbe96a3f2e24
[~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
76 /* can be called in from
77  * -- KIM (during fw download)
78  * -- ST Core (during st_write)
79  *
80  *  This is the internal write function - a wrapper
81  *  to tty->ops->write
82  */
83 int st_int_write(struct st_data_s *st_gdata,
84         const unsigned char *data, int count)
85 {
86         struct tty_struct *tty;
87         if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
88                 pr_err("tty unavailable to perform write");
89                 return -1;
90         }
91         tty = st_gdata->tty;
92 #ifdef VERBOSE
93         print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
94                 16, 1, data, count, 0);
95 #endif
96         return tty->ops->write(tty, data, count);
97
98 }
99
100 /*
101  * push the skb received to relevant
102  * protocol stacks
103  */
104 void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
105 {
106         pr_info(" %s(prot:%d) ", __func__, protoid);
107
108         if (unlikely
109             (st_gdata == NULL || st_gdata->rx_skb == NULL
110              || st_gdata->list[protoid] == NULL)) {
111                 pr_err("protocol %d not registered, no data to send?",
112                            protoid);
113                 kfree_skb(st_gdata->rx_skb);
114                 return;
115         }
116         /* this cannot fail
117          * this shouldn't take long
118          * - should be just skb_queue_tail for the
119          *   protocol stack driver
120          */
121         if (likely(st_gdata->list[protoid]->recv != NULL)) {
122                 if (unlikely(st_gdata->list[protoid]->recv(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(err);
148         }
149 }
150
151 static inline int st_check_data_len(struct st_data_s *st_gdata,
152         int protoid, int len)
153 {
154         register int room = skb_tailroom(st_gdata->rx_skb);
155
156         pr_debug("len %d room %d", len, room);
157
158         if (!len) {
159                 /* Received packet has only packet header and
160                  * has zero length payload. So, ask ST CORE to
161                  * forward the packet to protocol driver (BT/FM/GPS)
162                  */
163                 st_send_frame(protoid, st_gdata);
164
165         } else if (len > room) {
166                 /* Received packet's payload length is larger.
167                  * We can't accommodate it in created skb.
168                  */
169                 pr_err("Data length is too large len %d room %d", len,
170                            room);
171                 kfree_skb(st_gdata->rx_skb);
172         } else {
173                 /* Packet header has non-zero payload length and
174                  * we have enough space in created skb. Lets read
175                  * payload data */
176                 st_gdata->rx_state = ST_BT_W4_DATA;
177                 st_gdata->rx_count = len;
178                 return len;
179         }
180
181         /* Change ST state to continue to process next
182          * packet */
183         st_gdata->rx_state = ST_W4_PACKET_TYPE;
184         st_gdata->rx_skb = NULL;
185         st_gdata->rx_count = 0;
186
187         return 0;
188 }
189
190 /**
191  * st_wakeup_ack - internal function for action when wake-up ack
192  *      received
193  */
194 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
195         unsigned char cmd)
196 {
197         register struct sk_buff *waiting_skb;
198         unsigned long flags = 0;
199
200         spin_lock_irqsave(&st_gdata->lock, flags);
201         /* de-Q from waitQ and Q in txQ now that the
202          * chip is awake
203          */
204         while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
205                 skb_queue_tail(&st_gdata->txq, waiting_skb);
206
207         /* state forwarded to ST LL */
208         st_ll_sleep_state(st_gdata, (unsigned long)cmd);
209         spin_unlock_irqrestore(&st_gdata->lock, flags);
210
211         /* wake up to send the recently copied skbs from waitQ */
212         st_tx_wakeup(st_gdata);
213 }
214
215 /**
216  * st_int_recv - ST's internal receive function.
217  *      Decodes received RAW data and forwards to corresponding
218  *      client drivers (Bluetooth,FM,GPS..etc).
219  *      This can receive various types of packets,
220  *      HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
221  *      CH-8 packets from FM, CH-9 packets from GPS cores.
222  */
223 void st_int_recv(void *disc_data,
224         const unsigned char *data, long count)
225 {
226         register char *ptr;
227         struct hci_event_hdr *eh;
228         struct hci_acl_hdr *ah;
229         struct hci_sco_hdr *sh;
230         struct fm_event_hdr *fm;
231         struct gps_event_hdr *gps;
232         register int len = 0, type = 0, dlen = 0;
233         static enum proto_type protoid = ST_MAX;
234         struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
235
236         ptr = (char *)data;
237         /* tty_receive sent null ? */
238         if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
239                 pr_err(" received null from TTY ");
240                 return;
241         }
242
243         pr_info("count %ld rx_state %ld"
244                    "rx_count %ld", count, st_gdata->rx_state,
245                    st_gdata->rx_count);
246
247         /* Decode received bytes here */
248         while (count) {
249                 if (st_gdata->rx_count) {
250                         len = min_t(unsigned int, st_gdata->rx_count, count);
251                         memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
252                         st_gdata->rx_count -= len;
253                         count -= len;
254                         ptr += len;
255
256                         if (st_gdata->rx_count)
257                                 continue;
258
259                         /* Check ST RX state machine , where are we? */
260                         switch (st_gdata->rx_state) {
261
262                                 /* Waiting for complete packet ? */
263                         case ST_BT_W4_DATA:
264                                 pr_debug("Complete pkt received");
265
266                                 /* Ask ST CORE to forward
267                                  * the packet to protocol driver */
268                                 st_send_frame(protoid, st_gdata);
269
270                                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
271                                 st_gdata->rx_skb = NULL;
272                                 protoid = ST_MAX;       /* is this required ? */
273                                 continue;
274
275                                 /* Waiting for Bluetooth event header ? */
276                         case ST_BT_W4_EVENT_HDR:
277                                 eh = (struct hci_event_hdr *)st_gdata->rx_skb->
278                                     data;
279
280                                 pr_debug("Event header: evt 0x%2.2x"
281                                            "plen %d", eh->evt, eh->plen);
282
283                                 st_check_data_len(st_gdata, protoid, eh->plen);
284                                 continue;
285
286                                 /* Waiting for Bluetooth acl header ? */
287                         case ST_BT_W4_ACL_HDR:
288                                 ah = (struct hci_acl_hdr *)st_gdata->rx_skb->
289                                     data;
290                                 dlen = __le16_to_cpu(ah->dlen);
291
292                                 pr_info("ACL header: dlen %d", dlen);
293
294                                 st_check_data_len(st_gdata, protoid, dlen);
295                                 continue;
296
297                                 /* Waiting for Bluetooth sco header ? */
298                         case ST_BT_W4_SCO_HDR:
299                                 sh = (struct hci_sco_hdr *)st_gdata->rx_skb->
300                                     data;
301
302                                 pr_info("SCO header: dlen %d", sh->dlen);
303
304                                 st_check_data_len(st_gdata, protoid, sh->dlen);
305                                 continue;
306                         case ST_FM_W4_EVENT_HDR:
307                                 fm = (struct fm_event_hdr *)st_gdata->rx_skb->
308                                     data;
309                                 pr_info("FM Header: ");
310                                 st_check_data_len(st_gdata, ST_FM, fm->plen);
311                                 continue;
312                                 /* TODO : Add GPS packet machine logic here */
313                         case ST_GPS_W4_EVENT_HDR:
314                                 /* [0x09 pkt hdr][R/W byte][2 byte len] */
315                                 gps = (struct gps_event_hdr *)st_gdata->rx_skb->
316                                      data;
317                                 pr_info("GPS Header: ");
318                                 st_check_data_len(st_gdata, ST_GPS, gps->plen);
319                                 continue;
320                         }       /* end of switch rx_state */
321                 }
322
323                 /* end of if rx_count */
324                 /* Check first byte of packet and identify module
325                  * owner (BT/FM/GPS) */
326                 switch (*ptr) {
327
328                         /* Bluetooth event packet? */
329                 case HCI_EVENT_PKT:
330                         pr_info("Event packet");
331                         st_gdata->rx_state = ST_BT_W4_EVENT_HDR;
332                         st_gdata->rx_count = HCI_EVENT_HDR_SIZE;
333                         type = HCI_EVENT_PKT;
334                         protoid = ST_BT;
335                         break;
336
337                         /* Bluetooth acl packet? */
338                 case HCI_ACLDATA_PKT:
339                         pr_info("ACL packet");
340                         st_gdata->rx_state = ST_BT_W4_ACL_HDR;
341                         st_gdata->rx_count = HCI_ACL_HDR_SIZE;
342                         type = HCI_ACLDATA_PKT;
343                         protoid = ST_BT;
344                         break;
345
346                         /* Bluetooth sco packet? */
347                 case HCI_SCODATA_PKT:
348                         pr_info("SCO packet");
349                         st_gdata->rx_state = ST_BT_W4_SCO_HDR;
350                         st_gdata->rx_count = HCI_SCO_HDR_SIZE;
351                         type = HCI_SCODATA_PKT;
352                         protoid = ST_BT;
353                         break;
354
355                         /* Channel 8(FM) packet? */
356                 case ST_FM_CH8_PKT:
357                         pr_info("FM CH8 packet");
358                         type = ST_FM_CH8_PKT;
359                         st_gdata->rx_state = ST_FM_W4_EVENT_HDR;
360                         st_gdata->rx_count = FM_EVENT_HDR_SIZE;
361                         protoid = ST_FM;
362                         break;
363
364                         /* Channel 9(GPS) packet? */
365                 case 0x9:       /*ST_LL_GPS_CH9_PKT */
366                         pr_info("GPS CH9 packet");
367                         type = 0x9;     /* ST_LL_GPS_CH9_PKT; */
368                         protoid = ST_GPS;
369                         st_gdata->rx_state = ST_GPS_W4_EVENT_HDR;
370                         st_gdata->rx_count = 3; /* GPS_EVENT_HDR_SIZE -1*/
371                         break;
372                 case LL_SLEEP_IND:
373                 case LL_SLEEP_ACK:
374                 case LL_WAKE_UP_IND:
375                         pr_info("PM packet");
376                         /* this takes appropriate action based on
377                          * sleep state received --
378                          */
379                         st_ll_sleep_state(st_gdata, *ptr);
380                         ptr++;
381                         count--;
382                         continue;
383                 case LL_WAKE_UP_ACK:
384                         pr_info("PM packet");
385                         /* wake up ack received */
386                         st_wakeup_ack(st_gdata, *ptr);
387                         ptr++;
388                         count--;
389                         continue;
390                         /* Unknow packet? */
391                 default:
392                         pr_err("Unknown packet type %2.2x", (__u8) *ptr);
393                         ptr++;
394                         count--;
395                         continue;
396                 };
397                 ptr++;
398                 count--;
399
400                 switch (protoid) {
401                 case ST_BT:
402                         /* Allocate new packet to hold received data */
403                         st_gdata->rx_skb =
404                             bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
405                         if (!st_gdata->rx_skb) {
406                                 pr_err("Can't allocate mem for new packet");
407                                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
408                                 st_gdata->rx_count = 0;
409                                 return;
410                         }
411                         bt_cb(st_gdata->rx_skb)->pkt_type = type;
412                         break;
413                 case ST_FM:     /* for FM */
414                         st_gdata->rx_skb =
415                             alloc_skb(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
416                         if (!st_gdata->rx_skb) {
417                                 pr_err("Can't allocate mem for new packet");
418                                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
419                                 st_gdata->rx_count = 0;
420                                 return;
421                         }
422                         /* place holder 0x08 */
423                         skb_reserve(st_gdata->rx_skb, 1);
424                         st_gdata->rx_skb->cb[0] = ST_FM_CH8_PKT;
425                         break;
426                 case ST_GPS:
427                         /* for GPS */
428                         st_gdata->rx_skb =
429                             alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC);
430                         if (!st_gdata->rx_skb) {
431                                 pr_err("Can't allocate mem for new packet");
432                                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
433                                 st_gdata->rx_count = 0;
434                                 return;
435                         }
436                         /* place holder 0x09 */
437                         skb_reserve(st_gdata->rx_skb, 1);
438                         st_gdata->rx_skb->cb[0] = 0x09; /*ST_GPS_CH9_PKT; */
439                         break;
440                 case ST_MAX:
441                         break;
442                 }
443         }
444         pr_debug("done %s", __func__);
445         return;
446 }
447
448 /**
449  * st_int_dequeue - internal de-Q function.
450  *      If the previous data set was not written
451  *      completely, return that skb which has the pending data.
452  *      In normal cases, return top of txq.
453  */
454 struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
455 {
456         struct sk_buff *returning_skb;
457
458         pr_debug("%s", __func__);
459         if (st_gdata->tx_skb != NULL) {
460                 returning_skb = st_gdata->tx_skb;
461                 st_gdata->tx_skb = NULL;
462                 return returning_skb;
463         }
464         return skb_dequeue(&st_gdata->txq);
465 }
466
467 /**
468  * st_int_enqueue - internal Q-ing function.
469  *      Will either Q the skb to txq or the tx_waitq
470  *      depending on the ST LL state.
471  *      If the chip is asleep, then Q it onto waitq and
472  *      wakeup the chip.
473  *      txq and waitq needs protection since the other contexts
474  *      may be sending data, waking up chip.
475  */
476 void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
477 {
478         unsigned long flags = 0;
479
480         pr_debug("%s", __func__);
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:
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         case ST_LL_ASLEEP:
497                 skb_queue_tail(&st_gdata->tx_waitq, skb);
498                 st_ll_wakeup(st_gdata);
499                 break;
500         default:
501                 pr_err("ST LL is illegal state(%ld),"
502                            "purging received skb.", st_ll_getstate(st_gdata));
503                 kfree_skb(skb);
504                 break;
505         }
506
507         spin_unlock_irqrestore(&st_gdata->lock, flags);
508         pr_debug("done %s", __func__);
509         return;
510 }
511
512 /*
513  * internal wakeup function
514  * called from either
515  * - TTY layer when write's finished
516  * - st_write (in context of the protocol stack)
517  */
518 void st_tx_wakeup(struct st_data_s *st_data)
519 {
520         struct sk_buff *skb;
521         unsigned long flags;    /* for irq save flags */
522         pr_debug("%s", __func__);
523         /* check for sending & set flag sending here */
524         if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
525                 pr_info("ST already sending");
526                 /* keep sending */
527                 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
528                 return;
529                 /* TX_WAKEUP will be checked in another
530                  * context
531                  */
532         }
533         do {                    /* come back if st_tx_wakeup is set */
534                 /* woke-up to write */
535                 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
536                 while ((skb = st_int_dequeue(st_data))) {
537                         int len;
538                         spin_lock_irqsave(&st_data->lock, flags);
539                         /* enable wake-up from TTY */
540                         set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
541                         len = st_int_write(st_data, skb->data, skb->len);
542                         skb_pull(skb, len);
543                         /* if skb->len = len as expected, skb->len=0 */
544                         if (skb->len) {
545                                 /* would be the next skb to be sent */
546                                 st_data->tx_skb = skb;
547                                 spin_unlock_irqrestore(&st_data->lock, flags);
548                                 break;
549                         }
550                         kfree_skb(skb);
551                         spin_unlock_irqrestore(&st_data->lock, flags);
552                 }
553                 /* if wake-up is set in another context- restart sending */
554         } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
555
556         /* clear flag sending */
557         clear_bit(ST_TX_SENDING, &st_data->tx_state);
558 }
559
560 /********************************************************************/
561 /* functions called from ST KIM
562 */
563 void kim_st_list_protocols(struct st_data_s *st_gdata, char *buf)
564 {
565         unsigned long flags = 0;
566 #ifdef DEBUG
567         unsigned char i = ST_MAX;
568 #endif
569         spin_lock_irqsave(&st_gdata->lock, flags);
570 #ifdef DEBUG                    /* more detailed log */
571         for (i = 0; i < ST_MAX; i++) {
572                 if (i == 0) {
573                         sprintf(buf, "%s is %s", protocol_strngs[i],
574                                 st_gdata->list[i] !=
575                                 NULL ? "Registered" : "Unregistered");
576                 } else {
577                         sprintf(buf, "%s\n%s is %s", buf, protocol_strngs[i],
578                                 st_gdata->list[i] !=
579                                 NULL ? "Registered" : "Unregistered");
580                 }
581         }
582         sprintf(buf, "%s\n", buf);
583 #else /* limited info */
584         sprintf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
585                         st_gdata->protos_registered,
586                         st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
587                         st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
588                         st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
589 #endif
590         spin_unlock_irqrestore(&st_gdata->lock, flags);
591 }
592
593 /********************************************************************/
594 /*
595  * functions called from protocol stack drivers
596  * to be EXPORT-ed
597  */
598 long st_register(struct st_proto_s *new_proto)
599 {
600         struct st_data_s        *st_gdata;
601         long err = 0;
602         unsigned long flags = 0;
603
604         st_kim_ref(&st_gdata);
605         pr_info("%s(%d) ", __func__, new_proto->type);
606         if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
607             || new_proto->reg_complete_cb == NULL) {
608                 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
609                 return -1;
610         }
611
612         if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
613                 pr_err("protocol %d not supported", new_proto->type);
614                 return -EPROTONOSUPPORT;
615         }
616
617         if (st_gdata->list[new_proto->type] != NULL) {
618                 pr_err("protocol %d already registered", new_proto->type);
619                 return -EALREADY;
620         }
621
622         /* can be from process context only */
623         spin_lock_irqsave(&st_gdata->lock, flags);
624
625         if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
626                 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
627                 /* fw download in progress */
628                 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
629
630                 st_gdata->list[new_proto->type] = new_proto;
631                 st_gdata->protos_registered++;
632                 new_proto->write = st_write;
633
634                 set_bit(ST_REG_PENDING, &st_gdata->st_state);
635                 spin_unlock_irqrestore(&st_gdata->lock, flags);
636                 return -EINPROGRESS;
637         } else if (st_gdata->protos_registered == ST_EMPTY) {
638                 pr_info(" protocol list empty :%d ", new_proto->type);
639                 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
640                 st_recv = st_kim_recv;
641
642                 /* release lock previously held - re-locked below */
643                 spin_unlock_irqrestore(&st_gdata->lock, flags);
644
645                 /* enable the ST LL - to set default chip state */
646                 st_ll_enable(st_gdata);
647                 /* this may take a while to complete
648                  * since it involves BT fw download
649                  */
650                 err = st_kim_start(st_gdata->kim_data);
651                 if (err != 0) {
652                         clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
653                         if ((st_gdata->protos_registered != ST_EMPTY) &&
654                             (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
655                                 pr_err(" KIM failure complete callback ");
656                                 st_reg_complete(st_gdata, -1);
657                         }
658
659                         return -1;
660                 }
661
662                 /* the protocol might require other gpios to be toggled
663                  */
664                 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
665
666                 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
667                 st_recv = st_int_recv;
668
669                 /* this is where all pending registration
670                  * are signalled to be complete by calling callback functions
671                  */
672                 if ((st_gdata->protos_registered != ST_EMPTY) &&
673                     (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
674                         pr_debug(" call reg complete callback ");
675                         st_reg_complete(st_gdata, 0);
676                 }
677                 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
678
679                 /* check for already registered once more,
680                  * since the above check is old
681                  */
682                 if (st_gdata->list[new_proto->type] != NULL) {
683                         pr_err(" proto %d already registered ",
684                                    new_proto->type);
685                         return -EALREADY;
686                 }
687
688                 spin_lock_irqsave(&st_gdata->lock, flags);
689                 st_gdata->list[new_proto->type] = new_proto;
690                 st_gdata->protos_registered++;
691                 new_proto->write = st_write;
692                 spin_unlock_irqrestore(&st_gdata->lock, flags);
693                 return err;
694         }
695         /* if fw is already downloaded & new stack registers protocol */
696         else {
697                 switch (new_proto->type) {
698                 case ST_BT:
699                         /* do nothing */
700                         break;
701                 case ST_FM:
702                 case ST_GPS:
703                         st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
704                         break;
705                 case ST_MAX:
706                 default:
707                         pr_err("%d protocol not supported",
708                                    new_proto->type);
709                         err = -EPROTONOSUPPORT;
710                         /* something wrong */
711                         break;
712                 }
713                 st_gdata->list[new_proto->type] = new_proto;
714                 st_gdata->protos_registered++;
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_debug("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 = 0;
731         unsigned long flags = 0;
732         struct st_data_s        *st_gdata;
733
734         pr_debug("%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 -EPROTONOSUPPORT;
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 -EPROTONOSUPPORT;
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(st_gdata->kim_data);
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 -1;
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 -1;
814         }
815 #endif
816         pr_debug("%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 = 0;
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(st_gdata->kim_data);
859         pr_debug("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(st_gdata->kim_data);
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_debug("%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         print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
912                 16, 1, data, count, 0);
913 #endif
914
915         /*
916          * if fw download is in progress then route incoming data
917          * to KIM for validation
918          */
919         st_recv(tty->disc_data, data, count);
920         pr_debug("done %s", __func__);
921 }
922
923 /* wake-up function called in from the TTY layer
924  * inside the internal wakeup function will be called
925  */
926 static void st_tty_wakeup(struct tty_struct *tty)
927 {
928         struct  st_data_s *st_gdata = tty->disc_data;
929         pr_debug("%s ", __func__);
930         /* don't do an wakeup for now */
931         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
932
933         /* call our internal wakeup */
934         st_tx_wakeup((void *)st_gdata);
935 }
936
937 static void st_tty_flush_buffer(struct tty_struct *tty)
938 {
939         struct  st_data_s *st_gdata = tty->disc_data;
940         pr_debug("%s ", __func__);
941
942         kfree_skb(st_gdata->tx_skb);
943         st_gdata->tx_skb = NULL;
944
945         tty->ops->flush_buffer(tty);
946         return;
947 }
948
949 /********************************************************************/
950 int st_core_init(struct st_data_s **core_data)
951 {
952         struct st_data_s *st_gdata;
953         long err;
954         static struct tty_ldisc_ops *st_ldisc_ops;
955
956         /* populate and register to TTY line discipline */
957         st_ldisc_ops = kzalloc(sizeof(*st_ldisc_ops), GFP_KERNEL);
958         if (!st_ldisc_ops) {
959                 pr_err("no mem to allocate");
960                 return -ENOMEM;
961         }
962
963         st_ldisc_ops->magic = TTY_LDISC_MAGIC;
964         st_ldisc_ops->name = "n_st";    /*"n_hci"; */
965         st_ldisc_ops->open = st_tty_open;
966         st_ldisc_ops->close = st_tty_close;
967         st_ldisc_ops->receive_buf = st_tty_receive;
968         st_ldisc_ops->write_wakeup = st_tty_wakeup;
969         st_ldisc_ops->flush_buffer = st_tty_flush_buffer;
970         st_ldisc_ops->owner = THIS_MODULE;
971
972         err = tty_register_ldisc(N_TI_WL, st_ldisc_ops);
973         if (err) {
974                 pr_err("error registering %d line discipline %ld",
975                            N_TI_WL, err);
976                 kfree(st_ldisc_ops);
977                 return err;
978         }
979         pr_debug("registered n_shared line discipline");
980
981         st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
982         if (!st_gdata) {
983                 pr_err("memory allocation failed");
984                 err = tty_unregister_ldisc(N_TI_WL);
985                 if (err)
986                         pr_err("unable to un-register ldisc %ld", err);
987                 kfree(st_ldisc_ops);
988                 err = -ENOMEM;
989                 return err;
990         }
991
992         /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
993          * will be pushed in this queue for actual transmission.
994          */
995         skb_queue_head_init(&st_gdata->txq);
996         skb_queue_head_init(&st_gdata->tx_waitq);
997
998         /* Locking used in st_int_enqueue() to avoid multiple execution */
999         spin_lock_init(&st_gdata->lock);
1000
1001         /* ldisc_ops ref to be only used in __exit of module */
1002         st_gdata->ldisc_ops = st_ldisc_ops;
1003
1004 #if 0
1005         err = st_kim_init();
1006         if (err) {
1007                 pr_err("error during kim initialization(%ld)", err);
1008                 kfree(st_gdata);
1009                 err = tty_unregister_ldisc(N_TI_WL);
1010                 if (err)
1011                         pr_err("unable to un-register ldisc");
1012                 kfree(st_ldisc_ops);
1013                 return -1;
1014         }
1015 #endif
1016
1017         err = st_ll_init(st_gdata);
1018         if (err) {
1019                 pr_err("error during st_ll initialization(%ld)", err);
1020                 kfree(st_gdata);
1021                 err = tty_unregister_ldisc(N_TI_WL);
1022                 if (err)
1023                         pr_err("unable to un-register ldisc");
1024                 kfree(st_ldisc_ops);
1025                 return -1;
1026         }
1027         *core_data = st_gdata;
1028         return 0;
1029 }
1030
1031 void st_core_exit(struct st_data_s *st_gdata)
1032 {
1033         long err;
1034         /* internal module cleanup */
1035         err = st_ll_deinit(st_gdata);
1036         if (err)
1037                 pr_err("error during deinit of ST LL %ld", err);
1038 #if 0
1039         err = st_kim_deinit();
1040         if (err)
1041                 pr_err("error during deinit of ST KIM %ld", err);
1042 #endif
1043         if (st_gdata != NULL) {
1044                 /* Free ST Tx Qs and skbs */
1045                 skb_queue_purge(&st_gdata->txq);
1046                 skb_queue_purge(&st_gdata->tx_waitq);
1047                 kfree_skb(st_gdata->rx_skb);
1048                 kfree_skb(st_gdata->tx_skb);
1049                 /* TTY ldisc cleanup */
1050                 err = tty_unregister_ldisc(N_TI_WL);
1051                 if (err)
1052                         pr_err("unable to un-register ldisc %ld", err);
1053                 kfree(st_gdata->ldisc_ops);
1054                 /* free the global data pointer */
1055                 kfree(st_gdata);
1056         }
1057 }
1058
1059