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