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