Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Shared Transport Line discipline driver Core |
| 3 | * This hooks up ST KIM driver and ST LL driver |
Pavan Savoy | a0cc2f3 | 2010-10-06 12:18:14 -0400 | [diff] [blame] | 4 | * Copyright (C) 2009-2010 Texas Instruments |
| 5 | * Author: Pavan Savoy <pavan_savoy@ti.com> |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #define pr_fmt(fmt) "(stc): " fmt |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 25 | #include <linux/tty.h> |
| 26 | |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 27 | #include <linux/seq_file.h> |
| 28 | #include <linux/skbuff.h> |
| 29 | |
Pavan Savoy | e555867 | 2010-09-30 16:13:30 -0400 | [diff] [blame] | 30 | #include <linux/ti_wilink_st.h> |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 31 | |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 32 | extern void st_kim_recv(void *, const unsigned char *, long); |
| 33 | void st_int_recv(void *, const unsigned char *, long); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 34 | /* function pointer pointing to either, |
| 35 | * st_kim_recv during registration to receive fw download responses |
| 36 | * st_int_recv after registration to receive proto stack responses |
| 37 | */ |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 38 | static void (*st_recv) (void *, const unsigned char *, long); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 39 | |
| 40 | /********************************************************************/ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 41 | static void add_channel_to_table(struct st_data_s *st_gdata, |
| 42 | struct st_proto_s *new_proto) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 43 | { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 44 | pr_info("%s: id %d\n", __func__, new_proto->chnl_id); |
| 45 | /* list now has the channel id as index itself */ |
| 46 | st_gdata->list[new_proto->chnl_id] = new_proto; |
Pavan Savoy | 764b0c4 | 2011-04-08 04:57:42 -0500 | [diff] [blame] | 47 | st_gdata->is_registered[new_proto->chnl_id] = true; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 48 | } |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 49 | |
| 50 | static void remove_channel_from_table(struct st_data_s *st_gdata, |
| 51 | struct st_proto_s *proto) |
| 52 | { |
| 53 | pr_info("%s: id %d\n", __func__, proto->chnl_id); |
Pavan Savoy | 764b0c4 | 2011-04-08 04:57:42 -0500 | [diff] [blame] | 54 | /* st_gdata->list[proto->chnl_id] = NULL; */ |
| 55 | st_gdata->is_registered[proto->chnl_id] = false; |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 56 | } |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 57 | |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 58 | /* |
| 59 | * called from KIM during firmware download. |
| 60 | * |
| 61 | * This is a wrapper function to tty->ops->write_room. |
| 62 | * It returns number of free space available in |
| 63 | * uart tx buffer. |
| 64 | */ |
| 65 | int st_get_uart_wr_room(struct st_data_s *st_gdata) |
| 66 | { |
| 67 | struct tty_struct *tty; |
| 68 | if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) { |
| 69 | pr_err("tty unavailable to perform write"); |
| 70 | return -1; |
| 71 | } |
| 72 | tty = st_gdata->tty; |
| 73 | return tty->ops->write_room(tty); |
| 74 | } |
| 75 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 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 | { |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 86 | struct tty_struct *tty; |
| 87 | if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) { |
| 88 | pr_err("tty unavailable to perform write"); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 89 | return -EINVAL; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 90 | } |
| 91 | tty = st_gdata->tty; |
| 92 | #ifdef VERBOSE |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 93 | print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE, |
| 94 | 16, 1, data, count, 0); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 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 | */ |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 104 | static void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 105 | { |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 106 | pr_debug(" %s(prot:%d) ", __func__, chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 107 | |
| 108 | if (unlikely |
| 109 | (st_gdata == NULL || st_gdata->rx_skb == NULL |
Pavan Savoy | 764b0c4 | 2011-04-08 04:57:42 -0500 | [diff] [blame] | 110 | || st_gdata->is_registered[chnl_id] == false)) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 111 | pr_err("chnl_id %d not registered, no data to send?", |
| 112 | chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 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 | */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 121 | if (likely(st_gdata->list[chnl_id]->recv != NULL)) { |
Pavan Savoy | bb8f3c0 | 2010-07-22 05:32:07 -0500 | [diff] [blame] | 122 | if (unlikely |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 123 | (st_gdata->list[chnl_id]->recv |
| 124 | (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb) |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 125 | != 0)) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 126 | pr_err(" proto stack %d's ->recv failed", chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 127 | kfree_skb(st_gdata->rx_skb); |
| 128 | return; |
| 129 | } |
| 130 | } else { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 131 | pr_err(" proto stack %d's ->recv null", chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 132 | kfree_skb(st_gdata->rx_skb); |
| 133 | } |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 137 | /** |
| 138 | * st_reg_complete - |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 139 | * to call registration complete callbacks |
| 140 | * of all protocol stack drivers |
Pavan Savoy | bfb88d6 | 2011-12-15 10:38:20 -0600 | [diff] [blame] | 141 | * This function is being called with spin lock held, protocol drivers are |
| 142 | * only expected to complete their waits and do nothing more than that. |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 143 | */ |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 144 | static void st_reg_complete(struct st_data_s *st_gdata, char err) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 145 | { |
| 146 | unsigned char i = 0; |
| 147 | pr_info(" %s ", __func__); |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 148 | for (i = 0; i < ST_MAX_CHANNELS; i++) { |
Pavan Savoy | 764b0c4 | 2011-04-08 04:57:42 -0500 | [diff] [blame] | 149 | if (likely(st_gdata != NULL && |
| 150 | st_gdata->is_registered[i] == true && |
| 151 | st_gdata->list[i]->reg_complete_cb != NULL)) { |
Pavan Savoy | bb8f3c0 | 2010-07-22 05:32:07 -0500 | [diff] [blame] | 152 | st_gdata->list[i]->reg_complete_cb |
| 153 | (st_gdata->list[i]->priv_data, err); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 154 | pr_info("protocol %d's cb sent %d\n", i, err); |
| 155 | if (err) { /* cleanup registered protocol */ |
| 156 | st_gdata->protos_registered--; |
Pavan Savoy | 764b0c4 | 2011-04-08 04:57:42 -0500 | [diff] [blame] | 157 | st_gdata->is_registered[i] = false; |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 158 | } |
| 159 | } |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | static inline int st_check_data_len(struct st_data_s *st_gdata, |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 164 | unsigned char chnl_id, int len) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 165 | { |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 166 | int room = skb_tailroom(st_gdata->rx_skb); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 167 | |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 168 | pr_debug("len %d room %d", len, room); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 169 | |
| 170 | if (!len) { |
| 171 | /* Received packet has only packet header and |
| 172 | * has zero length payload. So, ask ST CORE to |
| 173 | * forward the packet to protocol driver (BT/FM/GPS) |
| 174 | */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 175 | st_send_frame(chnl_id, st_gdata); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 176 | |
| 177 | } else if (len > room) { |
| 178 | /* Received packet's payload length is larger. |
| 179 | * We can't accommodate it in created skb. |
| 180 | */ |
| 181 | pr_err("Data length is too large len %d room %d", len, |
| 182 | room); |
| 183 | kfree_skb(st_gdata->rx_skb); |
| 184 | } else { |
| 185 | /* Packet header has non-zero payload length and |
| 186 | * we have enough space in created skb. Lets read |
| 187 | * payload data */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 188 | st_gdata->rx_state = ST_W4_DATA; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 189 | st_gdata->rx_count = len; |
| 190 | return len; |
| 191 | } |
| 192 | |
| 193 | /* Change ST state to continue to process next |
| 194 | * packet */ |
| 195 | st_gdata->rx_state = ST_W4_PACKET_TYPE; |
| 196 | st_gdata->rx_skb = NULL; |
| 197 | st_gdata->rx_count = 0; |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 198 | st_gdata->rx_chnl = 0; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 203 | /** |
| 204 | * st_wakeup_ack - internal function for action when wake-up ack |
| 205 | * received |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 206 | */ |
| 207 | static inline void st_wakeup_ack(struct st_data_s *st_gdata, |
| 208 | unsigned char cmd) |
| 209 | { |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 210 | struct sk_buff *waiting_skb; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 211 | unsigned long flags = 0; |
| 212 | |
| 213 | spin_lock_irqsave(&st_gdata->lock, flags); |
| 214 | /* de-Q from waitQ and Q in txQ now that the |
| 215 | * chip is awake |
| 216 | */ |
| 217 | while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq))) |
| 218 | skb_queue_tail(&st_gdata->txq, waiting_skb); |
| 219 | |
| 220 | /* state forwarded to ST LL */ |
| 221 | st_ll_sleep_state(st_gdata, (unsigned long)cmd); |
| 222 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
| 223 | |
| 224 | /* wake up to send the recently copied skbs from waitQ */ |
| 225 | st_tx_wakeup(st_gdata); |
| 226 | } |
| 227 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 228 | /** |
| 229 | * st_int_recv - ST's internal receive function. |
| 230 | * Decodes received RAW data and forwards to corresponding |
| 231 | * client drivers (Bluetooth,FM,GPS..etc). |
| 232 | * This can receive various types of packets, |
| 233 | * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets |
| 234 | * CH-8 packets from FM, CH-9 packets from GPS cores. |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 235 | */ |
| 236 | void st_int_recv(void *disc_data, |
| 237 | const unsigned char *data, long count) |
| 238 | { |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 239 | char *ptr; |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 240 | struct st_proto_s *proto; |
| 241 | unsigned short payload_len = 0; |
channing | 25c22d5 | 2013-01-10 16:27:29 +0800 | [diff] [blame] | 242 | int len = 0; |
| 243 | unsigned char type = 0; |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 244 | unsigned char *plen; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 245 | struct st_data_s *st_gdata = (struct st_data_s *)disc_data; |
Pavan Savoy | 6d71ba2 | 2011-02-04 02:23:14 -0600 | [diff] [blame] | 246 | unsigned long flags; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 247 | |
| 248 | ptr = (char *)data; |
| 249 | /* tty_receive sent null ? */ |
| 250 | if (unlikely(ptr == NULL) || (st_gdata == NULL)) { |
| 251 | pr_err(" received null from TTY "); |
| 252 | return; |
| 253 | } |
| 254 | |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 255 | pr_debug("count %ld rx_state %ld" |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 256 | "rx_count %ld", count, st_gdata->rx_state, |
| 257 | st_gdata->rx_count); |
| 258 | |
Pavan Savoy | 6d71ba2 | 2011-02-04 02:23:14 -0600 | [diff] [blame] | 259 | spin_lock_irqsave(&st_gdata->lock, flags); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 260 | /* Decode received bytes here */ |
| 261 | while (count) { |
| 262 | if (st_gdata->rx_count) { |
| 263 | len = min_t(unsigned int, st_gdata->rx_count, count); |
| 264 | memcpy(skb_put(st_gdata->rx_skb, len), ptr, len); |
| 265 | st_gdata->rx_count -= len; |
| 266 | count -= len; |
| 267 | ptr += len; |
| 268 | |
| 269 | if (st_gdata->rx_count) |
| 270 | continue; |
| 271 | |
| 272 | /* Check ST RX state machine , where are we? */ |
| 273 | switch (st_gdata->rx_state) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 274 | /* Waiting for complete packet ? */ |
| 275 | case ST_W4_DATA: |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 276 | pr_debug("Complete pkt received"); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 277 | /* Ask ST CORE to forward |
| 278 | * the packet to protocol driver */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 279 | st_send_frame(st_gdata->rx_chnl, st_gdata); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 280 | |
| 281 | st_gdata->rx_state = ST_W4_PACKET_TYPE; |
| 282 | st_gdata->rx_skb = NULL; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 283 | continue; |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 284 | /* parse the header to know details */ |
| 285 | case ST_W4_HEADER: |
| 286 | proto = st_gdata->list[st_gdata->rx_chnl]; |
| 287 | plen = |
| 288 | &st_gdata->rx_skb->data |
| 289 | [proto->offset_len_in_hdr]; |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 290 | pr_debug("plen pointing to %x\n", *plen); |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 291 | if (proto->len_size == 1)/* 1 byte len field */ |
| 292 | payload_len = *(unsigned char *)plen; |
| 293 | else if (proto->len_size == 2) |
| 294 | payload_len = |
| 295 | __le16_to_cpu(*(unsigned short *)plen); |
| 296 | else |
| 297 | pr_info("%s: invalid length " |
| 298 | "for id %d\n", |
| 299 | __func__, proto->chnl_id); |
| 300 | st_check_data_len(st_gdata, proto->chnl_id, |
| 301 | payload_len); |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 302 | pr_debug("off %d, pay len %d\n", |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 303 | proto->offset_len_in_hdr, payload_len); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 304 | continue; |
| 305 | } /* end of switch rx_state */ |
| 306 | } |
| 307 | |
| 308 | /* end of if rx_count */ |
| 309 | /* Check first byte of packet and identify module |
| 310 | * owner (BT/FM/GPS) */ |
| 311 | switch (*ptr) { |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 312 | case LL_SLEEP_IND: |
| 313 | case LL_SLEEP_ACK: |
| 314 | case LL_WAKE_UP_IND: |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 315 | pr_debug("PM packet"); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 316 | /* this takes appropriate action based on |
| 317 | * sleep state received -- |
| 318 | */ |
| 319 | st_ll_sleep_state(st_gdata, *ptr); |
Pavan Savoy | 6d71ba2 | 2011-02-04 02:23:14 -0600 | [diff] [blame] | 320 | /* if WAKEUP_IND collides copy from waitq to txq |
| 321 | * and assume chip awake |
| 322 | */ |
| 323 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
| 324 | if (st_ll_getstate(st_gdata) == ST_LL_AWAKE) |
| 325 | st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK); |
| 326 | spin_lock_irqsave(&st_gdata->lock, flags); |
| 327 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 328 | ptr++; |
| 329 | count--; |
| 330 | continue; |
| 331 | case LL_WAKE_UP_ACK: |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 332 | pr_debug("PM packet"); |
Pavan Savoy | 6d71ba2 | 2011-02-04 02:23:14 -0600 | [diff] [blame] | 333 | |
| 334 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 335 | /* wake up ack received */ |
| 336 | st_wakeup_ack(st_gdata, *ptr); |
Pavan Savoy | 6d71ba2 | 2011-02-04 02:23:14 -0600 | [diff] [blame] | 337 | spin_lock_irqsave(&st_gdata->lock, flags); |
| 338 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 339 | ptr++; |
| 340 | count--; |
| 341 | continue; |
| 342 | /* Unknow packet? */ |
| 343 | default: |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 344 | type = *ptr; |
Vijay Badawadagi | 78bb969 | 2011-08-10 10:18:33 -0500 | [diff] [blame] | 345 | if (st_gdata->list[type] == NULL) { |
| 346 | pr_err("chip/interface misbehavior dropping" |
| 347 | " frame starting with 0x%02x", type); |
| 348 | goto done; |
| 349 | |
| 350 | } |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 351 | st_gdata->rx_skb = alloc_skb( |
| 352 | st_gdata->list[type]->max_frame_size, |
| 353 | GFP_ATOMIC); |
Alan Cox | 5353cf0 | 2012-07-30 14:40:06 -0700 | [diff] [blame] | 354 | if (st_gdata->rx_skb == NULL) { |
| 355 | pr_err("out of memory: dropping\n"); |
| 356 | goto done; |
| 357 | } |
| 358 | |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 359 | skb_reserve(st_gdata->rx_skb, |
| 360 | st_gdata->list[type]->reserve); |
| 361 | /* next 2 required for BT only */ |
| 362 | st_gdata->rx_skb->cb[0] = type; /*pkt_type*/ |
| 363 | st_gdata->rx_skb->cb[1] = 0; /*incoming*/ |
| 364 | st_gdata->rx_chnl = *ptr; |
| 365 | st_gdata->rx_state = ST_W4_HEADER; |
| 366 | st_gdata->rx_count = st_gdata->list[type]->hdr_len; |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 367 | pr_debug("rx_count %ld\n", st_gdata->rx_count); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 368 | }; |
| 369 | ptr++; |
| 370 | count--; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 371 | } |
Vijay Badawadagi | 78bb969 | 2011-08-10 10:18:33 -0500 | [diff] [blame] | 372 | done: |
Pavan Savoy | 6d71ba2 | 2011-02-04 02:23:14 -0600 | [diff] [blame] | 373 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 374 | pr_debug("done %s", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 378 | /** |
| 379 | * st_int_dequeue - internal de-Q function. |
| 380 | * If the previous data set was not written |
| 381 | * completely, return that skb which has the pending data. |
| 382 | * In normal cases, return top of txq. |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 383 | */ |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 384 | static struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 385 | { |
| 386 | struct sk_buff *returning_skb; |
| 387 | |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 388 | pr_debug("%s", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 389 | if (st_gdata->tx_skb != NULL) { |
| 390 | returning_skb = st_gdata->tx_skb; |
| 391 | st_gdata->tx_skb = NULL; |
| 392 | return returning_skb; |
| 393 | } |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 394 | return skb_dequeue(&st_gdata->txq); |
| 395 | } |
| 396 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 397 | /** |
| 398 | * st_int_enqueue - internal Q-ing function. |
| 399 | * Will either Q the skb to txq or the tx_waitq |
| 400 | * depending on the ST LL state. |
| 401 | * If the chip is asleep, then Q it onto waitq and |
| 402 | * wakeup the chip. |
| 403 | * txq and waitq needs protection since the other contexts |
| 404 | * may be sending data, waking up chip. |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 405 | */ |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 406 | static void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 407 | { |
| 408 | unsigned long flags = 0; |
| 409 | |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 410 | pr_debug("%s", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 411 | spin_lock_irqsave(&st_gdata->lock, flags); |
| 412 | |
| 413 | switch (st_ll_getstate(st_gdata)) { |
| 414 | case ST_LL_AWAKE: |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 415 | pr_debug("ST LL is AWAKE, sending normally"); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 416 | skb_queue_tail(&st_gdata->txq, skb); |
| 417 | break; |
| 418 | case ST_LL_ASLEEP_TO_AWAKE: |
| 419 | skb_queue_tail(&st_gdata->tx_waitq, skb); |
| 420 | break; |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 421 | case ST_LL_AWAKE_TO_ASLEEP: |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 422 | pr_err("ST LL is illegal state(%ld)," |
| 423 | "purging received skb.", st_ll_getstate(st_gdata)); |
| 424 | kfree_skb(skb); |
| 425 | break; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 426 | case ST_LL_ASLEEP: |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 427 | skb_queue_tail(&st_gdata->tx_waitq, skb); |
| 428 | st_ll_wakeup(st_gdata); |
| 429 | break; |
| 430 | default: |
| 431 | pr_err("ST LL is illegal state(%ld)," |
| 432 | "purging received skb.", st_ll_getstate(st_gdata)); |
| 433 | kfree_skb(skb); |
| 434 | break; |
| 435 | } |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 436 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 437 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 438 | pr_debug("done %s", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * internal wakeup function |
| 444 | * called from either |
| 445 | * - TTY layer when write's finished |
| 446 | * - st_write (in context of the protocol stack) |
| 447 | */ |
| 448 | void st_tx_wakeup(struct st_data_s *st_data) |
| 449 | { |
| 450 | struct sk_buff *skb; |
| 451 | unsigned long flags; /* for irq save flags */ |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 452 | pr_debug("%s", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 453 | /* check for sending & set flag sending here */ |
| 454 | if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) { |
Pavan Savoy | 6710fcf | 2011-02-04 02:23:12 -0600 | [diff] [blame] | 455 | pr_debug("ST already sending"); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 456 | /* keep sending */ |
| 457 | set_bit(ST_TX_WAKEUP, &st_data->tx_state); |
| 458 | return; |
| 459 | /* TX_WAKEUP will be checked in another |
| 460 | * context |
| 461 | */ |
| 462 | } |
| 463 | do { /* come back if st_tx_wakeup is set */ |
| 464 | /* woke-up to write */ |
| 465 | clear_bit(ST_TX_WAKEUP, &st_data->tx_state); |
| 466 | while ((skb = st_int_dequeue(st_data))) { |
| 467 | int len; |
| 468 | spin_lock_irqsave(&st_data->lock, flags); |
| 469 | /* enable wake-up from TTY */ |
| 470 | set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags); |
| 471 | len = st_int_write(st_data, skb->data, skb->len); |
| 472 | skb_pull(skb, len); |
| 473 | /* if skb->len = len as expected, skb->len=0 */ |
| 474 | if (skb->len) { |
| 475 | /* would be the next skb to be sent */ |
| 476 | st_data->tx_skb = skb; |
| 477 | spin_unlock_irqrestore(&st_data->lock, flags); |
| 478 | break; |
| 479 | } |
| 480 | kfree_skb(skb); |
| 481 | spin_unlock_irqrestore(&st_data->lock, flags); |
| 482 | } |
| 483 | /* if wake-up is set in another context- restart sending */ |
| 484 | } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state)); |
| 485 | |
| 486 | /* clear flag sending */ |
| 487 | clear_bit(ST_TX_SENDING, &st_data->tx_state); |
| 488 | } |
| 489 | |
| 490 | /********************************************************************/ |
| 491 | /* functions called from ST KIM |
| 492 | */ |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 493 | void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 494 | { |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 495 | seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n", |
Naveen Jain | 36e574f | 2010-06-09 07:20:40 -0500 | [diff] [blame] | 496 | st_gdata->protos_registered, |
Pavan Savoy | 764b0c4 | 2011-04-08 04:57:42 -0500 | [diff] [blame] | 497 | st_gdata->is_registered[0x04] == true ? 'R' : 'U', |
| 498 | st_gdata->is_registered[0x08] == true ? 'R' : 'U', |
| 499 | st_gdata->is_registered[0x09] == true ? 'R' : 'U'); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /********************************************************************/ |
| 503 | /* |
| 504 | * functions called from protocol stack drivers |
| 505 | * to be EXPORT-ed |
| 506 | */ |
| 507 | long st_register(struct st_proto_s *new_proto) |
| 508 | { |
| 509 | struct st_data_s *st_gdata; |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 510 | long err = 0; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 511 | unsigned long flags = 0; |
| 512 | |
Pavan Savoy | dbd3a87 | 2010-08-19 14:08:51 -0400 | [diff] [blame] | 513 | st_kim_ref(&st_gdata, 0); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 514 | if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL |
| 515 | || new_proto->reg_complete_cb == NULL) { |
| 516 | pr_err("gdata/new_proto/recv or reg_complete_cb not ready"); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 517 | return -EINVAL; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 518 | } |
| 519 | |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 520 | if (new_proto->chnl_id >= ST_MAX_CHANNELS) { |
| 521 | pr_err("chnl_id %d not supported", new_proto->chnl_id); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 522 | return -EPROTONOSUPPORT; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Pavan Savoy | 764b0c4 | 2011-04-08 04:57:42 -0500 | [diff] [blame] | 525 | if (st_gdata->is_registered[new_proto->chnl_id] == true) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 526 | pr_err("chnl_id %d already registered", new_proto->chnl_id); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 527 | return -EALREADY; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* can be from process context only */ |
| 531 | spin_lock_irqsave(&st_gdata->lock, flags); |
| 532 | |
| 533 | if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 534 | pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 535 | /* fw download in progress */ |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 536 | |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 537 | add_channel_to_table(st_gdata, new_proto); |
Naveen Jain | 36e574f | 2010-06-09 07:20:40 -0500 | [diff] [blame] | 538 | st_gdata->protos_registered++; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 539 | new_proto->write = st_write; |
| 540 | |
| 541 | set_bit(ST_REG_PENDING, &st_gdata->st_state); |
| 542 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 543 | return -EINPROGRESS; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 544 | } else if (st_gdata->protos_registered == ST_EMPTY) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 545 | pr_info(" chnl_id list empty :%d ", new_proto->chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 546 | set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state); |
| 547 | st_recv = st_kim_recv; |
| 548 | |
Pavan Savoy | bfb88d6 | 2011-12-15 10:38:20 -0600 | [diff] [blame] | 549 | /* enable the ST LL - to set default chip state */ |
| 550 | st_ll_enable(st_gdata); |
| 551 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 552 | /* release lock previously held - re-locked below */ |
| 553 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
| 554 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 555 | /* this may take a while to complete |
| 556 | * since it involves BT fw download |
| 557 | */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 558 | err = st_kim_start(st_gdata->kim_data); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 559 | if (err != 0) { |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 560 | clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state); |
| 561 | if ((st_gdata->protos_registered != ST_EMPTY) && |
| 562 | (test_bit(ST_REG_PENDING, &st_gdata->st_state))) { |
| 563 | pr_err(" KIM failure complete callback "); |
Oleksandr Kozaruk | 3a2d3d2 | 2013-08-29 10:55:48 +0300 | [diff] [blame] | 564 | spin_lock_irqsave(&st_gdata->lock, flags); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 565 | st_reg_complete(st_gdata, err); |
Oleksandr Kozaruk | 3a2d3d2 | 2013-08-29 10:55:48 +0300 | [diff] [blame] | 566 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
Pavan Savoy | bfb88d6 | 2011-12-15 10:38:20 -0600 | [diff] [blame] | 567 | clear_bit(ST_REG_PENDING, &st_gdata->st_state); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 568 | } |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 569 | return -EINVAL; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 570 | } |
| 571 | |
Pavan Savoy | bfb88d6 | 2011-12-15 10:38:20 -0600 | [diff] [blame] | 572 | spin_lock_irqsave(&st_gdata->lock, flags); |
| 573 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 574 | clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state); |
| 575 | st_recv = st_int_recv; |
| 576 | |
| 577 | /* this is where all pending registration |
| 578 | * are signalled to be complete by calling callback functions |
| 579 | */ |
| 580 | if ((st_gdata->protos_registered != ST_EMPTY) && |
| 581 | (test_bit(ST_REG_PENDING, &st_gdata->st_state))) { |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 582 | pr_debug(" call reg complete callback "); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 583 | st_reg_complete(st_gdata, 0); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 584 | } |
| 585 | clear_bit(ST_REG_PENDING, &st_gdata->st_state); |
| 586 | |
| 587 | /* check for already registered once more, |
| 588 | * since the above check is old |
| 589 | */ |
Pavan Savoy | 764b0c4 | 2011-04-08 04:57:42 -0500 | [diff] [blame] | 590 | if (st_gdata->is_registered[new_proto->chnl_id] == true) { |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 591 | pr_err(" proto %d already registered ", |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 592 | new_proto->chnl_id); |
Pavan Savoy | bfb88d6 | 2011-12-15 10:38:20 -0600 | [diff] [blame] | 593 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 594 | return -EALREADY; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 595 | } |
| 596 | |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 597 | add_channel_to_table(st_gdata, new_proto); |
Naveen Jain | 36e574f | 2010-06-09 07:20:40 -0500 | [diff] [blame] | 598 | st_gdata->protos_registered++; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 599 | new_proto->write = st_write; |
| 600 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
| 601 | return err; |
| 602 | } |
| 603 | /* if fw is already downloaded & new stack registers protocol */ |
| 604 | else { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 605 | add_channel_to_table(st_gdata, new_proto); |
Naveen Jain | 36e574f | 2010-06-09 07:20:40 -0500 | [diff] [blame] | 606 | st_gdata->protos_registered++; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 607 | new_proto->write = st_write; |
| 608 | |
| 609 | /* lock already held before entering else */ |
| 610 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
| 611 | return err; |
| 612 | } |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 613 | pr_debug("done %s(%d) ", __func__, new_proto->chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 614 | } |
| 615 | EXPORT_SYMBOL_GPL(st_register); |
| 616 | |
| 617 | /* to unregister a protocol - |
| 618 | * to be called from protocol stack driver |
| 619 | */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 620 | long st_unregister(struct st_proto_s *proto) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 621 | { |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 622 | long err = 0; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 623 | unsigned long flags = 0; |
| 624 | struct st_data_s *st_gdata; |
| 625 | |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 626 | pr_debug("%s: %d ", __func__, proto->chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 627 | |
Pavan Savoy | dbd3a87 | 2010-08-19 14:08:51 -0400 | [diff] [blame] | 628 | st_kim_ref(&st_gdata, 0); |
Steven Rostedt | 7316a9f | 2011-05-23 16:45:32 -0400 | [diff] [blame] | 629 | if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 630 | pr_err(" chnl_id %d not supported", proto->chnl_id); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 631 | return -EPROTONOSUPPORT; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | spin_lock_irqsave(&st_gdata->lock, flags); |
| 635 | |
Pavan Savoy | bfb88d6 | 2011-12-15 10:38:20 -0600 | [diff] [blame] | 636 | if (st_gdata->is_registered[proto->chnl_id] == false) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 637 | pr_err(" chnl_id %d not registered", proto->chnl_id); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 638 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 639 | return -EPROTONOSUPPORT; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | st_gdata->protos_registered--; |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 643 | remove_channel_from_table(st_gdata, proto); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 644 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
| 645 | |
Pavan Savoy | bfb88d6 | 2011-12-15 10:38:20 -0600 | [diff] [blame] | 646 | /* paranoid check */ |
| 647 | if (st_gdata->protos_registered < ST_EMPTY) |
| 648 | st_gdata->protos_registered = ST_EMPTY; |
| 649 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 650 | if ((st_gdata->protos_registered == ST_EMPTY) && |
| 651 | (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 652 | pr_info(" all chnl_ids unregistered "); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 653 | |
| 654 | /* stop traffic on tty */ |
| 655 | if (st_gdata->tty) { |
| 656 | tty_ldisc_flush(st_gdata->tty); |
| 657 | stop_tty(st_gdata->tty); |
| 658 | } |
| 659 | |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 660 | /* all chnl_ids now unregistered */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 661 | st_kim_stop(st_gdata->kim_data); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 662 | /* disable ST LL */ |
| 663 | st_ll_disable(st_gdata); |
| 664 | } |
| 665 | return err; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * called in protocol stack drivers |
| 670 | * via the write function pointer |
| 671 | */ |
| 672 | long st_write(struct sk_buff *skb) |
| 673 | { |
| 674 | struct st_data_s *st_gdata; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 675 | long len; |
| 676 | |
Pavan Savoy | dbd3a87 | 2010-08-19 14:08:51 -0400 | [diff] [blame] | 677 | st_kim_ref(&st_gdata, 0); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 678 | if (unlikely(skb == NULL || st_gdata == NULL |
| 679 | || st_gdata->tty == NULL)) { |
| 680 | pr_err("data/tty unavailable to perform write"); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 681 | return -EINVAL; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 682 | } |
Pavan Savoy | c1605f2 | 2011-03-02 03:59:56 -0600 | [diff] [blame] | 683 | |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 684 | pr_debug("%d to be written", skb->len); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 685 | len = skb->len; |
| 686 | |
| 687 | /* st_ll to decide where to enqueue the skb */ |
| 688 | st_int_enqueue(st_gdata, skb); |
| 689 | /* wake up */ |
| 690 | st_tx_wakeup(st_gdata); |
| 691 | |
| 692 | /* return number of bytes written */ |
| 693 | return len; |
| 694 | } |
| 695 | |
| 696 | /* for protocols making use of shared transport */ |
| 697 | EXPORT_SYMBOL_GPL(st_unregister); |
| 698 | |
| 699 | /********************************************************************/ |
| 700 | /* |
| 701 | * functions called from TTY layer |
| 702 | */ |
| 703 | static int st_tty_open(struct tty_struct *tty) |
| 704 | { |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 705 | int err = 0; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 706 | struct st_data_s *st_gdata; |
| 707 | pr_info("%s ", __func__); |
| 708 | |
Pavan Savoy | dbd3a87 | 2010-08-19 14:08:51 -0400 | [diff] [blame] | 709 | st_kim_ref(&st_gdata, 0); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 710 | st_gdata->tty = tty; |
| 711 | tty->disc_data = st_gdata; |
| 712 | |
| 713 | /* don't do an wakeup for now */ |
| 714 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
| 715 | |
| 716 | /* mem already allocated |
| 717 | */ |
| 718 | tty->receive_room = 65536; |
| 719 | /* Flush any pending characters in the driver and discipline. */ |
| 720 | tty_ldisc_flush(tty); |
| 721 | tty_driver_flush_buffer(tty); |
| 722 | /* |
| 723 | * signal to UIM via KIM that - |
| 724 | * installation of N_TI_WL ldisc is complete |
| 725 | */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 726 | st_kim_complete(st_gdata->kim_data); |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 727 | pr_debug("done %s", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 728 | return err; |
| 729 | } |
| 730 | |
| 731 | static void st_tty_close(struct tty_struct *tty) |
| 732 | { |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 733 | unsigned char i = ST_MAX_CHANNELS; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 734 | unsigned long flags = 0; |
| 735 | struct st_data_s *st_gdata = tty->disc_data; |
| 736 | |
| 737 | pr_info("%s ", __func__); |
| 738 | |
| 739 | /* TODO: |
| 740 | * if a protocol has been registered & line discipline |
| 741 | * un-installed for some reason - what should be done ? |
| 742 | */ |
| 743 | spin_lock_irqsave(&st_gdata->lock, flags); |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 744 | for (i = ST_BT; i < ST_MAX_CHANNELS; i++) { |
Pavan Savoy | 5926cef | 2011-08-10 10:18:30 -0500 | [diff] [blame] | 745 | if (st_gdata->is_registered[i] == true) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 746 | pr_err("%d not un-registered", i); |
| 747 | st_gdata->list[i] = NULL; |
Pavan Savoy | 651d62a | 2011-08-10 10:18:37 -0500 | [diff] [blame] | 748 | st_gdata->is_registered[i] = false; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 749 | } |
Pavan Savoy | bb8f3c0 | 2010-07-22 05:32:07 -0500 | [diff] [blame] | 750 | st_gdata->protos_registered = 0; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 751 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
| 752 | /* |
| 753 | * signal to UIM via KIM that - |
| 754 | * N_TI_WL ldisc is un-installed |
| 755 | */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 756 | st_kim_complete(st_gdata->kim_data); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 757 | st_gdata->tty = NULL; |
| 758 | /* Flush any pending characters in the driver and discipline. */ |
| 759 | tty_ldisc_flush(tty); |
| 760 | tty_driver_flush_buffer(tty); |
| 761 | |
| 762 | spin_lock_irqsave(&st_gdata->lock, flags); |
| 763 | /* empty out txq and tx_waitq */ |
| 764 | skb_queue_purge(&st_gdata->txq); |
| 765 | skb_queue_purge(&st_gdata->tx_waitq); |
| 766 | /* reset the TTY Rx states of ST */ |
| 767 | st_gdata->rx_count = 0; |
| 768 | st_gdata->rx_state = ST_W4_PACKET_TYPE; |
| 769 | kfree_skb(st_gdata->rx_skb); |
| 770 | st_gdata->rx_skb = NULL; |
| 771 | spin_unlock_irqrestore(&st_gdata->lock, flags); |
| 772 | |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 773 | pr_debug("%s: done ", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 774 | } |
| 775 | |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 776 | static void st_tty_receive(struct tty_struct *tty, const unsigned char *data, |
| 777 | char *tty_flags, int count) |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 778 | { |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 779 | #ifdef VERBOSE |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 780 | print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE, |
| 781 | 16, 1, data, count, 0); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 782 | #endif |
| 783 | |
| 784 | /* |
| 785 | * if fw download is in progress then route incoming data |
| 786 | * to KIM for validation |
| 787 | */ |
| 788 | st_recv(tty->disc_data, data, count); |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 789 | pr_debug("done %s", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* wake-up function called in from the TTY layer |
| 793 | * inside the internal wakeup function will be called |
| 794 | */ |
| 795 | static void st_tty_wakeup(struct tty_struct *tty) |
| 796 | { |
| 797 | struct st_data_s *st_gdata = tty->disc_data; |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 798 | pr_debug("%s ", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 799 | /* don't do an wakeup for now */ |
| 800 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
| 801 | |
| 802 | /* call our internal wakeup */ |
| 803 | st_tx_wakeup((void *)st_gdata); |
| 804 | } |
| 805 | |
| 806 | static void st_tty_flush_buffer(struct tty_struct *tty) |
| 807 | { |
| 808 | struct st_data_s *st_gdata = tty->disc_data; |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 809 | pr_debug("%s ", __func__); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 810 | |
| 811 | kfree_skb(st_gdata->tx_skb); |
| 812 | st_gdata->tx_skb = NULL; |
| 813 | |
Peter Hurley | 94459a9 | 2013-11-29 18:00:35 -0500 | [diff] [blame] | 814 | tty_driver_flush_buffer(tty); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 815 | return; |
| 816 | } |
| 817 | |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 818 | static struct tty_ldisc_ops st_ldisc_ops = { |
| 819 | .magic = TTY_LDISC_MAGIC, |
| 820 | .name = "n_st", |
| 821 | .open = st_tty_open, |
| 822 | .close = st_tty_close, |
| 823 | .receive_buf = st_tty_receive, |
| 824 | .write_wakeup = st_tty_wakeup, |
| 825 | .flush_buffer = st_tty_flush_buffer, |
| 826 | .owner = THIS_MODULE |
| 827 | }; |
| 828 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 829 | /********************************************************************/ |
| 830 | int st_core_init(struct st_data_s **core_data) |
| 831 | { |
| 832 | struct st_data_s *st_gdata; |
| 833 | long err; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 834 | |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 835 | err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 836 | if (err) { |
| 837 | pr_err("error registering %d line discipline %ld", |
| 838 | N_TI_WL, err); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 839 | return err; |
| 840 | } |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 841 | pr_debug("registered n_shared line discipline"); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 842 | |
| 843 | st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL); |
| 844 | if (!st_gdata) { |
| 845 | pr_err("memory allocation failed"); |
| 846 | err = tty_unregister_ldisc(N_TI_WL); |
| 847 | if (err) |
| 848 | pr_err("unable to un-register ldisc %ld", err); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 849 | err = -ENOMEM; |
| 850 | return err; |
| 851 | } |
| 852 | |
| 853 | /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's |
| 854 | * will be pushed in this queue for actual transmission. |
| 855 | */ |
| 856 | skb_queue_head_init(&st_gdata->txq); |
| 857 | skb_queue_head_init(&st_gdata->tx_waitq); |
| 858 | |
| 859 | /* Locking used in st_int_enqueue() to avoid multiple execution */ |
| 860 | spin_lock_init(&st_gdata->lock); |
| 861 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 862 | err = st_ll_init(st_gdata); |
| 863 | if (err) { |
| 864 | pr_err("error during st_ll initialization(%ld)", err); |
| 865 | kfree(st_gdata); |
| 866 | err = tty_unregister_ldisc(N_TI_WL); |
| 867 | if (err) |
| 868 | pr_err("unable to un-register ldisc"); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 869 | return err; |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 870 | } |
| 871 | *core_data = st_gdata; |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | void st_core_exit(struct st_data_s *st_gdata) |
| 876 | { |
| 877 | long err; |
| 878 | /* internal module cleanup */ |
| 879 | err = st_ll_deinit(st_gdata); |
| 880 | if (err) |
| 881 | pr_err("error during deinit of ST LL %ld", err); |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 882 | |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 883 | if (st_gdata != NULL) { |
| 884 | /* Free ST Tx Qs and skbs */ |
| 885 | skb_queue_purge(&st_gdata->txq); |
| 886 | skb_queue_purge(&st_gdata->tx_waitq); |
| 887 | kfree_skb(st_gdata->rx_skb); |
| 888 | kfree_skb(st_gdata->tx_skb); |
| 889 | /* TTY ldisc cleanup */ |
| 890 | err = tty_unregister_ldisc(N_TI_WL); |
| 891 | if (err) |
| 892 | pr_err("unable to un-register ldisc %ld", err); |
Pavan Savoy | 53618cc | 2010-04-08 13:16:53 -0500 | [diff] [blame] | 893 | /* free the global data pointer */ |
| 894 | kfree(st_gdata); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | |