Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Shared Transport Line discipline driver Core
0004  *  This hooks up ST KIM driver and ST LL driver
0005  *  Copyright (C) 2009-2010 Texas Instruments
0006  *  Author: Pavan Savoy <pavan_savoy@ti.com>
0007  */
0008 
0009 #define pr_fmt(fmt) "(stc): " fmt
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/tty.h>
0013 
0014 #include <linux/seq_file.h>
0015 #include <linux/skbuff.h>
0016 
0017 #include <linux/ti_wilink_st.h>
0018 
0019 extern void st_kim_recv(void *, const unsigned char *, long);
0020 void st_int_recv(void *, const unsigned char *, long);
0021 /*
0022  * function pointer pointing to either,
0023  * st_kim_recv during registration to receive fw download responses
0024  * st_int_recv after registration to receive proto stack responses
0025  */
0026 static void (*st_recv) (void *, const unsigned char *, long);
0027 
0028 /********************************************************************/
0029 static void add_channel_to_table(struct st_data_s *st_gdata,
0030         struct st_proto_s *new_proto)
0031 {
0032     pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
0033     /* list now has the channel id as index itself */
0034     st_gdata->list[new_proto->chnl_id] = new_proto;
0035     st_gdata->is_registered[new_proto->chnl_id] = true;
0036 }
0037 
0038 static void remove_channel_from_table(struct st_data_s *st_gdata,
0039         struct st_proto_s *proto)
0040 {
0041     pr_info("%s: id %d\n", __func__, proto->chnl_id);
0042 /*  st_gdata->list[proto->chnl_id] = NULL; */
0043     st_gdata->is_registered[proto->chnl_id] = false;
0044 }
0045 
0046 /*
0047  * called from KIM during firmware download.
0048  *
0049  * This is a wrapper function to tty->ops->write_room.
0050  * It returns number of free space available in
0051  * uart tx buffer.
0052  */
0053 int st_get_uart_wr_room(struct st_data_s *st_gdata)
0054 {
0055     if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
0056         pr_err("tty unavailable to perform write");
0057         return -1;
0058     }
0059 
0060     return tty_write_room(st_gdata->tty);
0061 }
0062 
0063 /*
0064  * can be called in from
0065  * -- KIM (during fw download)
0066  * -- ST Core (during st_write)
0067  *
0068  *  This is the internal write function - a wrapper
0069  *  to tty->ops->write
0070  */
0071 int st_int_write(struct st_data_s *st_gdata,
0072     const unsigned char *data, int count)
0073 {
0074     struct tty_struct *tty;
0075     if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
0076         pr_err("tty unavailable to perform write");
0077         return -EINVAL;
0078     }
0079     tty = st_gdata->tty;
0080 #ifdef VERBOSE
0081     print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
0082         16, 1, data, count, 0);
0083 #endif
0084     return tty->ops->write(tty, data, count);
0085 
0086 }
0087 
0088 /*
0089  * push the skb received to relevant
0090  * protocol stacks
0091  */
0092 static void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
0093 {
0094     pr_debug(" %s(prot:%d) ", __func__, chnl_id);
0095 
0096     if (unlikely
0097         (st_gdata == NULL || st_gdata->rx_skb == NULL
0098          || st_gdata->is_registered[chnl_id] == false)) {
0099         pr_err("chnl_id %d not registered, no data to send?",
0100                chnl_id);
0101         kfree_skb(st_gdata->rx_skb);
0102         return;
0103     }
0104     /*
0105      * this cannot fail
0106      * this shouldn't take long
0107      * - should be just skb_queue_tail for the
0108      *   protocol stack driver
0109      */
0110     if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
0111         if (unlikely
0112             (st_gdata->list[chnl_id]->recv
0113             (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
0114                  != 0)) {
0115             pr_err(" proto stack %d's ->recv failed", chnl_id);
0116             kfree_skb(st_gdata->rx_skb);
0117             return;
0118         }
0119     } else {
0120         pr_err(" proto stack %d's ->recv null", chnl_id);
0121         kfree_skb(st_gdata->rx_skb);
0122     }
0123     return;
0124 }
0125 
0126 /*
0127  * st_reg_complete - to call registration complete callbacks
0128  * of all protocol stack drivers
0129  * This function is being called with spin lock held, protocol drivers are
0130  * only expected to complete their waits and do nothing more than that.
0131  */
0132 static void st_reg_complete(struct st_data_s *st_gdata, int err)
0133 {
0134     unsigned char i = 0;
0135     pr_info(" %s ", __func__);
0136     for (i = 0; i < ST_MAX_CHANNELS; i++) {
0137         if (likely(st_gdata != NULL &&
0138             st_gdata->is_registered[i] == true &&
0139                 st_gdata->list[i]->reg_complete_cb != NULL)) {
0140             st_gdata->list[i]->reg_complete_cb
0141                 (st_gdata->list[i]->priv_data, err);
0142             pr_info("protocol %d's cb sent %d\n", i, err);
0143             if (err) { /* cleanup registered protocol */
0144                 st_gdata->is_registered[i] = false;
0145                 if (st_gdata->protos_registered)
0146                     st_gdata->protos_registered--;
0147             }
0148         }
0149     }
0150 }
0151 
0152 static inline int st_check_data_len(struct st_data_s *st_gdata,
0153     unsigned char chnl_id, int len)
0154 {
0155     int room = skb_tailroom(st_gdata->rx_skb);
0156 
0157     pr_debug("len %d room %d", len, room);
0158 
0159     if (!len) {
0160         /*
0161          * Received packet has only packet header and
0162          * has zero length payload. So, ask ST CORE to
0163          * forward the packet to protocol driver (BT/FM/GPS)
0164          */
0165         st_send_frame(chnl_id, st_gdata);
0166 
0167     } else if (len > room) {
0168         /*
0169          * Received packet's payload length is larger.
0170          * We can't accommodate it in created skb.
0171          */
0172         pr_err("Data length is too large len %d room %d", len,
0173                room);
0174         kfree_skb(st_gdata->rx_skb);
0175     } else {
0176         /*
0177          * Packet header has non-zero payload length and
0178          * we have enough space in created skb. Lets read
0179          * payload data */
0180         st_gdata->rx_state = ST_W4_DATA;
0181         st_gdata->rx_count = len;
0182         return len;
0183     }
0184 
0185     /* Change ST state to continue to process next packet */
0186     st_gdata->rx_state = ST_W4_PACKET_TYPE;
0187     st_gdata->rx_skb = NULL;
0188     st_gdata->rx_count = 0;
0189     st_gdata->rx_chnl = 0;
0190 
0191     return 0;
0192 }
0193 
0194 /*
0195  * st_wakeup_ack - internal function for action when wake-up ack
0196  *  received
0197  */
0198 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
0199     unsigned char cmd)
0200 {
0201     struct sk_buff *waiting_skb;
0202     unsigned long flags = 0;
0203 
0204     spin_lock_irqsave(&st_gdata->lock, flags);
0205     /*
0206      * de-Q from waitQ and Q in txQ now that the
0207      * chip is awake
0208      */
0209     while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
0210         skb_queue_tail(&st_gdata->txq, waiting_skb);
0211 
0212     /* state forwarded to ST LL */
0213     st_ll_sleep_state(st_gdata, (unsigned long)cmd);
0214     spin_unlock_irqrestore(&st_gdata->lock, flags);
0215 
0216     /* wake up to send the recently copied skbs from waitQ */
0217     st_tx_wakeup(st_gdata);
0218 }
0219 
0220 /*
0221  * st_int_recv - ST's internal receive function.
0222  *  Decodes received RAW data and forwards to corresponding
0223  *  client drivers (Bluetooth,FM,GPS..etc).
0224  *  This can receive various types of packets,
0225  *  HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
0226  *  CH-8 packets from FM, CH-9 packets from GPS cores.
0227  */
0228 void st_int_recv(void *disc_data,
0229     const unsigned char *data, long count)
0230 {
0231     char *ptr;
0232     struct st_proto_s *proto;
0233     unsigned short payload_len = 0;
0234     int len = 0;
0235     unsigned char type = 0;
0236     unsigned char *plen;
0237     struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
0238     unsigned long flags;
0239 
0240     ptr = (char *)data;
0241     /* tty_receive sent null ? */
0242     if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
0243         pr_err(" received null from TTY ");
0244         return;
0245     }
0246 
0247     pr_debug("count %ld rx_state %ld"
0248            "rx_count %ld", count, st_gdata->rx_state,
0249            st_gdata->rx_count);
0250 
0251     spin_lock_irqsave(&st_gdata->lock, flags);
0252     /* Decode received bytes here */
0253     while (count) {
0254         if (st_gdata->rx_count) {
0255             len = min_t(unsigned int, st_gdata->rx_count, count);
0256             skb_put_data(st_gdata->rx_skb, ptr, len);
0257             st_gdata->rx_count -= len;
0258             count -= len;
0259             ptr += len;
0260 
0261             if (st_gdata->rx_count)
0262                 continue;
0263 
0264             /* Check ST RX state machine , where are we? */
0265             switch (st_gdata->rx_state) {
0266             /* Waiting for complete packet ? */
0267             case ST_W4_DATA:
0268                 pr_debug("Complete pkt received");
0269                 /*
0270                  * Ask ST CORE to forward
0271                  * the packet to protocol driver
0272                  */
0273                 st_send_frame(st_gdata->rx_chnl, st_gdata);
0274 
0275                 st_gdata->rx_state = ST_W4_PACKET_TYPE;
0276                 st_gdata->rx_skb = NULL;
0277                 continue;
0278             /* parse the header to know details */
0279             case ST_W4_HEADER:
0280                 proto = st_gdata->list[st_gdata->rx_chnl];
0281                 plen =
0282                 &st_gdata->rx_skb->data
0283                 [proto->offset_len_in_hdr];
0284                 pr_debug("plen pointing to %x\n", *plen);
0285                 if (proto->len_size == 1) /* 1 byte len field */
0286                     payload_len = *(unsigned char *)plen;
0287                 else if (proto->len_size == 2)
0288                     payload_len =
0289                     __le16_to_cpu(*(unsigned short *)plen);
0290                 else
0291                     pr_info("%s: invalid length "
0292                     "for id %d\n",
0293                     __func__, proto->chnl_id);
0294                 st_check_data_len(st_gdata, proto->chnl_id,
0295                         payload_len);
0296                 pr_debug("off %d, pay len %d\n",
0297                     proto->offset_len_in_hdr, payload_len);
0298                 continue;
0299             }   /* end of switch rx_state */
0300         }
0301 
0302         /* end of if rx_count */
0303 
0304         /*
0305          * Check first byte of packet and identify module
0306          * owner (BT/FM/GPS)
0307          */
0308         switch (*ptr) {
0309         case LL_SLEEP_IND:
0310         case LL_SLEEP_ACK:
0311         case LL_WAKE_UP_IND:
0312             pr_debug("PM packet");
0313             /*
0314              * this takes appropriate action based on
0315              * sleep state received --
0316              */
0317             st_ll_sleep_state(st_gdata, *ptr);
0318             /*
0319              * if WAKEUP_IND collides copy from waitq to txq
0320              * and assume chip awake
0321              */
0322             spin_unlock_irqrestore(&st_gdata->lock, flags);
0323             if (st_ll_getstate(st_gdata) == ST_LL_AWAKE)
0324                 st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK);
0325             spin_lock_irqsave(&st_gdata->lock, flags);
0326 
0327             ptr++;
0328             count--;
0329             continue;
0330         case LL_WAKE_UP_ACK:
0331             pr_debug("PM packet");
0332 
0333             spin_unlock_irqrestore(&st_gdata->lock, flags);
0334             /* wake up ack received */
0335             st_wakeup_ack(st_gdata, *ptr);
0336             spin_lock_irqsave(&st_gdata->lock, flags);
0337 
0338             ptr++;
0339             count--;
0340             continue;
0341             /* Unknow packet? */
0342         default:
0343             type = *ptr;
0344 
0345             /*
0346              * Default case means non-HCILL packets,
0347              * possibilities are packets for:
0348              * (a) valid protocol -  Supported Protocols within
0349              *     the ST_MAX_CHANNELS.
0350              * (b) registered protocol - Checked by
0351              *     "st_gdata->list[type] == NULL)" are supported
0352              *     protocols only.
0353              *  Rules out any invalid protocol and
0354              *  unregistered protocols with channel ID < 16.
0355              */
0356 
0357             if ((type >= ST_MAX_CHANNELS) ||
0358                     (st_gdata->list[type] == NULL)) {
0359                 pr_err("chip/interface misbehavior: "
0360                         "dropping frame starting "
0361                         "with 0x%02x\n", type);
0362                 goto done;
0363             }
0364 
0365             st_gdata->rx_skb = alloc_skb(
0366                     st_gdata->list[type]->max_frame_size,
0367                     GFP_ATOMIC);
0368             if (st_gdata->rx_skb == NULL) {
0369                 pr_err("out of memory: dropping\n");
0370                 goto done;
0371             }
0372 
0373             skb_reserve(st_gdata->rx_skb,
0374                     st_gdata->list[type]->reserve);
0375             /* next 2 required for BT only */
0376             st_gdata->rx_skb->cb[0] = type; /*pkt_type*/
0377             st_gdata->rx_skb->cb[1] = 0; /*incoming*/
0378             st_gdata->rx_chnl = *ptr;
0379             st_gdata->rx_state = ST_W4_HEADER;
0380             st_gdata->rx_count = st_gdata->list[type]->hdr_len;
0381             pr_debug("rx_count %ld\n", st_gdata->rx_count);
0382         }
0383         ptr++;
0384         count--;
0385     }
0386 done:
0387     spin_unlock_irqrestore(&st_gdata->lock, flags);
0388     pr_debug("done %s", __func__);
0389     return;
0390 }
0391 
0392 /*
0393  * st_int_dequeue - internal de-Q function.
0394  *  If the previous data set was not written
0395  *  completely, return that skb which has the pending data.
0396  *  In normal cases, return top of txq.
0397  */
0398 static struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
0399 {
0400     struct sk_buff *returning_skb;
0401 
0402     pr_debug("%s", __func__);
0403     if (st_gdata->tx_skb != NULL) {
0404         returning_skb = st_gdata->tx_skb;
0405         st_gdata->tx_skb = NULL;
0406         return returning_skb;
0407     }
0408     return skb_dequeue(&st_gdata->txq);
0409 }
0410 
0411 /*
0412  * st_int_enqueue - internal Q-ing function.
0413  *  Will either Q the skb to txq or the tx_waitq
0414  *  depending on the ST LL state.
0415  *  If the chip is asleep, then Q it onto waitq and
0416  *  wakeup the chip.
0417  *  txq and waitq needs protection since the other contexts
0418  *  may be sending data, waking up chip.
0419  */
0420 static void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
0421 {
0422     unsigned long flags = 0;
0423 
0424     pr_debug("%s", __func__);
0425     spin_lock_irqsave(&st_gdata->lock, flags);
0426 
0427     switch (st_ll_getstate(st_gdata)) {
0428     case ST_LL_AWAKE:
0429         pr_debug("ST LL is AWAKE, sending normally");
0430         skb_queue_tail(&st_gdata->txq, skb);
0431         break;
0432     case ST_LL_ASLEEP_TO_AWAKE:
0433         skb_queue_tail(&st_gdata->tx_waitq, skb);
0434         break;
0435     case ST_LL_AWAKE_TO_ASLEEP:
0436         pr_err("ST LL is illegal state(%ld),"
0437                "purging received skb.", st_ll_getstate(st_gdata));
0438         kfree_skb(skb);
0439         break;
0440     case ST_LL_ASLEEP:
0441         skb_queue_tail(&st_gdata->tx_waitq, skb);
0442         st_ll_wakeup(st_gdata);
0443         break;
0444     default:
0445         pr_err("ST LL is illegal state(%ld),"
0446                "purging received skb.", st_ll_getstate(st_gdata));
0447         kfree_skb(skb);
0448         break;
0449     }
0450 
0451     spin_unlock_irqrestore(&st_gdata->lock, flags);
0452     pr_debug("done %s", __func__);
0453     return;
0454 }
0455 
0456 /*
0457  * internal wakeup function
0458  * called from either
0459  * - TTY layer when write's finished
0460  * - st_write (in context of the protocol stack)
0461  */
0462 static void work_fn_write_wakeup(struct work_struct *work)
0463 {
0464     struct st_data_s *st_gdata = container_of(work, struct st_data_s,
0465             work_write_wakeup);
0466 
0467     st_tx_wakeup((void *)st_gdata);
0468 }
0469 void st_tx_wakeup(struct st_data_s *st_data)
0470 {
0471     struct sk_buff *skb;
0472     unsigned long flags;    /* for irq save flags */
0473     pr_debug("%s", __func__);
0474     /* check for sending & set flag sending here */
0475     if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
0476         pr_debug("ST already sending");
0477         /* keep sending */
0478         set_bit(ST_TX_WAKEUP, &st_data->tx_state);
0479         return;
0480         /* TX_WAKEUP will be checked in another
0481          * context
0482          */
0483     }
0484     do {            /* come back if st_tx_wakeup is set */
0485         /* woke-up to write */
0486         clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
0487         while ((skb = st_int_dequeue(st_data))) {
0488             int len;
0489             spin_lock_irqsave(&st_data->lock, flags);
0490             /* enable wake-up from TTY */
0491             set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
0492             len = st_int_write(st_data, skb->data, skb->len);
0493             skb_pull(skb, len);
0494             /* if skb->len = len as expected, skb->len=0 */
0495             if (skb->len) {
0496                 /* would be the next skb to be sent */
0497                 st_data->tx_skb = skb;
0498                 spin_unlock_irqrestore(&st_data->lock, flags);
0499                 break;
0500             }
0501             kfree_skb(skb);
0502             spin_unlock_irqrestore(&st_data->lock, flags);
0503         }
0504         /* if wake-up is set in another context- restart sending */
0505     } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
0506 
0507     /* clear flag sending */
0508     clear_bit(ST_TX_SENDING, &st_data->tx_state);
0509 }
0510 
0511 /********************************************************************/
0512 /* functions called from ST KIM
0513 */
0514 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
0515 {
0516     seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
0517             st_gdata->protos_registered,
0518             st_gdata->is_registered[0x04] == true ? 'R' : 'U',
0519             st_gdata->is_registered[0x08] == true ? 'R' : 'U',
0520             st_gdata->is_registered[0x09] == true ? 'R' : 'U');
0521 }
0522 
0523 /********************************************************************/
0524 /*
0525  * functions called from protocol stack drivers
0526  * to be EXPORT-ed
0527  */
0528 long st_register(struct st_proto_s *new_proto)
0529 {
0530     struct st_data_s    *st_gdata;
0531     long err = 0;
0532     unsigned long flags = 0;
0533 
0534     st_kim_ref(&st_gdata, 0);
0535     if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
0536         || new_proto->reg_complete_cb == NULL) {
0537         pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
0538         return -EINVAL;
0539     }
0540 
0541     if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
0542         pr_err("chnl_id %d not supported", new_proto->chnl_id);
0543         return -EPROTONOSUPPORT;
0544     }
0545 
0546     if (st_gdata->is_registered[new_proto->chnl_id] == true) {
0547         pr_err("chnl_id %d already registered", new_proto->chnl_id);
0548         return -EALREADY;
0549     }
0550 
0551     /* can be from process context only */
0552     spin_lock_irqsave(&st_gdata->lock, flags);
0553 
0554     if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
0555         pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
0556         /* fw download in progress */
0557 
0558         add_channel_to_table(st_gdata, new_proto);
0559         st_gdata->protos_registered++;
0560         new_proto->write = st_write;
0561 
0562         set_bit(ST_REG_PENDING, &st_gdata->st_state);
0563         spin_unlock_irqrestore(&st_gdata->lock, flags);
0564         return -EINPROGRESS;
0565     } else if (st_gdata->protos_registered == ST_EMPTY) {
0566         pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
0567         set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
0568         st_recv = st_kim_recv;
0569 
0570         /* enable the ST LL - to set default chip state */
0571         st_ll_enable(st_gdata);
0572 
0573         /* release lock previously held - re-locked below */
0574         spin_unlock_irqrestore(&st_gdata->lock, flags);
0575 
0576         /*
0577          * this may take a while to complete
0578          * since it involves BT fw download
0579          */
0580         err = st_kim_start(st_gdata->kim_data);
0581         if (err != 0) {
0582             clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
0583             if ((st_gdata->protos_registered != ST_EMPTY) &&
0584                 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
0585                 pr_err(" KIM failure complete callback ");
0586                 spin_lock_irqsave(&st_gdata->lock, flags);
0587                 st_reg_complete(st_gdata, err);
0588                 spin_unlock_irqrestore(&st_gdata->lock, flags);
0589                 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
0590             }
0591             return -EINVAL;
0592         }
0593 
0594         spin_lock_irqsave(&st_gdata->lock, flags);
0595 
0596         clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
0597         st_recv = st_int_recv;
0598 
0599         /*
0600          * this is where all pending registration
0601          * are signalled to be complete by calling callback functions
0602          */
0603         if ((st_gdata->protos_registered != ST_EMPTY) &&
0604             (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
0605             pr_debug(" call reg complete callback ");
0606             st_reg_complete(st_gdata, 0);
0607         }
0608         clear_bit(ST_REG_PENDING, &st_gdata->st_state);
0609 
0610         /*
0611          * check for already registered once more,
0612          * since the above check is old
0613          */
0614         if (st_gdata->is_registered[new_proto->chnl_id] == true) {
0615             pr_err(" proto %d already registered ",
0616                    new_proto->chnl_id);
0617             spin_unlock_irqrestore(&st_gdata->lock, flags);
0618             return -EALREADY;
0619         }
0620 
0621         add_channel_to_table(st_gdata, new_proto);
0622         st_gdata->protos_registered++;
0623         new_proto->write = st_write;
0624         spin_unlock_irqrestore(&st_gdata->lock, flags);
0625         return err;
0626     }
0627     /* if fw is already downloaded & new stack registers protocol */
0628     else {
0629         add_channel_to_table(st_gdata, new_proto);
0630         st_gdata->protos_registered++;
0631         new_proto->write = st_write;
0632 
0633         /* lock already held before entering else */
0634         spin_unlock_irqrestore(&st_gdata->lock, flags);
0635         return err;
0636     }
0637 }
0638 EXPORT_SYMBOL_GPL(st_register);
0639 
0640 /*
0641  * to unregister a protocol -
0642  * to be called from protocol stack driver
0643  */
0644 long st_unregister(struct st_proto_s *proto)
0645 {
0646     long err = 0;
0647     unsigned long flags = 0;
0648     struct st_data_s    *st_gdata;
0649 
0650     pr_debug("%s: %d ", __func__, proto->chnl_id);
0651 
0652     st_kim_ref(&st_gdata, 0);
0653     if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
0654         pr_err(" chnl_id %d not supported", proto->chnl_id);
0655         return -EPROTONOSUPPORT;
0656     }
0657 
0658     spin_lock_irqsave(&st_gdata->lock, flags);
0659 
0660     if (st_gdata->is_registered[proto->chnl_id] == false) {
0661         pr_err(" chnl_id %d not registered", proto->chnl_id);
0662         spin_unlock_irqrestore(&st_gdata->lock, flags);
0663         return -EPROTONOSUPPORT;
0664     }
0665 
0666     if (st_gdata->protos_registered)
0667         st_gdata->protos_registered--;
0668 
0669     remove_channel_from_table(st_gdata, proto);
0670     spin_unlock_irqrestore(&st_gdata->lock, flags);
0671 
0672     if ((st_gdata->protos_registered == ST_EMPTY) &&
0673         (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
0674         pr_info(" all chnl_ids unregistered ");
0675 
0676         /* stop traffic on tty */
0677         if (st_gdata->tty) {
0678             tty_ldisc_flush(st_gdata->tty);
0679             stop_tty(st_gdata->tty);
0680         }
0681 
0682         /* all chnl_ids now unregistered */
0683         st_kim_stop(st_gdata->kim_data);
0684         /* disable ST LL */
0685         st_ll_disable(st_gdata);
0686     }
0687     return err;
0688 }
0689 
0690 /*
0691  * called in protocol stack drivers
0692  * via the write function pointer
0693  */
0694 long st_write(struct sk_buff *skb)
0695 {
0696     struct st_data_s *st_gdata;
0697     long len;
0698 
0699     st_kim_ref(&st_gdata, 0);
0700     if (unlikely(skb == NULL || st_gdata == NULL
0701         || st_gdata->tty == NULL)) {
0702         pr_err("data/tty unavailable to perform write");
0703         return -EINVAL;
0704     }
0705 
0706     pr_debug("%d to be written", skb->len);
0707     len = skb->len;
0708 
0709     /* st_ll to decide where to enqueue the skb */
0710     st_int_enqueue(st_gdata, skb);
0711     /* wake up */
0712     st_tx_wakeup(st_gdata);
0713 
0714     /* return number of bytes written */
0715     return len;
0716 }
0717 
0718 /* for protocols making use of shared transport */
0719 EXPORT_SYMBOL_GPL(st_unregister);
0720 
0721 /********************************************************************/
0722 /*
0723  * functions called from TTY layer
0724  */
0725 static int st_tty_open(struct tty_struct *tty)
0726 {
0727     struct st_data_s *st_gdata;
0728     pr_info("%s ", __func__);
0729 
0730     st_kim_ref(&st_gdata, 0);
0731     st_gdata->tty = tty;
0732     tty->disc_data = st_gdata;
0733 
0734     /* don't do an wakeup for now */
0735     clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0736 
0737     /* mem already allocated
0738      */
0739     tty->receive_room = 65536;
0740     /* Flush any pending characters in the driver and discipline. */
0741     tty_ldisc_flush(tty);
0742     tty_driver_flush_buffer(tty);
0743     /*
0744      * signal to UIM via KIM that -
0745      * installation of N_TI_WL ldisc is complete
0746      */
0747     st_kim_complete(st_gdata->kim_data);
0748     pr_debug("done %s", __func__);
0749 
0750     return 0;
0751 }
0752 
0753 static void st_tty_close(struct tty_struct *tty)
0754 {
0755     unsigned char i;
0756     unsigned long flags;
0757     struct  st_data_s *st_gdata = tty->disc_data;
0758 
0759     pr_info("%s ", __func__);
0760 
0761     /*
0762      * TODO:
0763      * if a protocol has been registered & line discipline
0764      * un-installed for some reason - what should be done ?
0765      */
0766     spin_lock_irqsave(&st_gdata->lock, flags);
0767     for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
0768         if (st_gdata->is_registered[i] == true)
0769             pr_err("%d not un-registered", i);
0770         st_gdata->list[i] = NULL;
0771         st_gdata->is_registered[i] = false;
0772     }
0773     st_gdata->protos_registered = 0;
0774     spin_unlock_irqrestore(&st_gdata->lock, flags);
0775     /*
0776      * signal to UIM via KIM that -
0777      * N_TI_WL ldisc is un-installed
0778      */
0779     st_kim_complete(st_gdata->kim_data);
0780     st_gdata->tty = NULL;
0781     /* Flush any pending characters in the driver and discipline. */
0782     tty_ldisc_flush(tty);
0783     tty_driver_flush_buffer(tty);
0784 
0785     spin_lock_irqsave(&st_gdata->lock, flags);
0786     /* empty out txq and tx_waitq */
0787     skb_queue_purge(&st_gdata->txq);
0788     skb_queue_purge(&st_gdata->tx_waitq);
0789     /* reset the TTY Rx states of ST */
0790     st_gdata->rx_count = 0;
0791     st_gdata->rx_state = ST_W4_PACKET_TYPE;
0792     kfree_skb(st_gdata->rx_skb);
0793     st_gdata->rx_skb = NULL;
0794     spin_unlock_irqrestore(&st_gdata->lock, flags);
0795 
0796     pr_debug("%s: done ", __func__);
0797 }
0798 
0799 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
0800                const char *tty_flags, int count)
0801 {
0802 #ifdef VERBOSE
0803     print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
0804         16, 1, data, count, 0);
0805 #endif
0806 
0807     /*
0808      * if fw download is in progress then route incoming data
0809      * to KIM for validation
0810      */
0811     st_recv(tty->disc_data, data, count);
0812     pr_debug("done %s", __func__);
0813 }
0814 
0815 /*
0816  * wake-up function called in from the TTY layer
0817  * inside the internal wakeup function will be called
0818  */
0819 static void st_tty_wakeup(struct tty_struct *tty)
0820 {
0821     struct  st_data_s *st_gdata = tty->disc_data;
0822     pr_debug("%s ", __func__);
0823     /* don't do an wakeup for now */
0824     clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0825 
0826     /*
0827      * schedule the internal wakeup instead of calling directly to
0828      * avoid lockup (port->lock needed in tty->ops->write is
0829      * already taken here
0830      */
0831     schedule_work(&st_gdata->work_write_wakeup);
0832 }
0833 
0834 static void st_tty_flush_buffer(struct tty_struct *tty)
0835 {
0836     struct  st_data_s *st_gdata = tty->disc_data;
0837     pr_debug("%s ", __func__);
0838 
0839     kfree_skb(st_gdata->tx_skb);
0840     st_gdata->tx_skb = NULL;
0841 
0842     tty_driver_flush_buffer(tty);
0843     return;
0844 }
0845 
0846 static struct tty_ldisc_ops st_ldisc_ops = {
0847     .num = N_TI_WL,
0848     .name = "n_st",
0849     .open = st_tty_open,
0850     .close = st_tty_close,
0851     .receive_buf = st_tty_receive,
0852     .write_wakeup = st_tty_wakeup,
0853     .flush_buffer = st_tty_flush_buffer,
0854     .owner = THIS_MODULE
0855 };
0856 
0857 /********************************************************************/
0858 int st_core_init(struct st_data_s **core_data)
0859 {
0860     struct st_data_s *st_gdata;
0861     long err;
0862 
0863     err = tty_register_ldisc(&st_ldisc_ops);
0864     if (err) {
0865         pr_err("error registering %d line discipline %ld",
0866                N_TI_WL, err);
0867         return err;
0868     }
0869     pr_debug("registered n_shared line discipline");
0870 
0871     st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
0872     if (!st_gdata) {
0873         pr_err("memory allocation failed");
0874         err = -ENOMEM;
0875         goto err_unreg_ldisc;
0876     }
0877 
0878     /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
0879      * will be pushed in this queue for actual transmission.
0880      */
0881     skb_queue_head_init(&st_gdata->txq);
0882     skb_queue_head_init(&st_gdata->tx_waitq);
0883 
0884     /* Locking used in st_int_enqueue() to avoid multiple execution */
0885     spin_lock_init(&st_gdata->lock);
0886 
0887     err = st_ll_init(st_gdata);
0888     if (err) {
0889         pr_err("error during st_ll initialization(%ld)", err);
0890         goto err_free_gdata;
0891     }
0892 
0893     INIT_WORK(&st_gdata->work_write_wakeup, work_fn_write_wakeup);
0894 
0895     *core_data = st_gdata;
0896     return 0;
0897 err_free_gdata:
0898     kfree(st_gdata);
0899 err_unreg_ldisc:
0900     tty_unregister_ldisc(&st_ldisc_ops);
0901     return err;
0902 }
0903 
0904 void st_core_exit(struct st_data_s *st_gdata)
0905 {
0906     long err;
0907     /* internal module cleanup */
0908     err = st_ll_deinit(st_gdata);
0909     if (err)
0910         pr_err("error during deinit of ST LL %ld", err);
0911 
0912     if (st_gdata != NULL) {
0913         /* Free ST Tx Qs and skbs */
0914         skb_queue_purge(&st_gdata->txq);
0915         skb_queue_purge(&st_gdata->tx_waitq);
0916         kfree_skb(st_gdata->rx_skb);
0917         kfree_skb(st_gdata->tx_skb);
0918         /* TTY ldisc cleanup */
0919         tty_unregister_ldisc(&st_ldisc_ops);
0920         /* free the global data pointer */
0921         kfree(st_gdata);
0922     }
0923 }