Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Marvell Bluetooth driver |
| 3 | * |
| 4 | * Copyright (C) 2009, Marvell International Ltd. |
| 5 | * |
| 6 | * This software file (the "File") is distributed by Marvell International |
| 7 | * Ltd. under the terms of the GNU General Public License Version 2, June 1991 |
| 8 | * (the "License"). You may use, redistribute and/or modify this File in |
| 9 | * accordance with the terms and conditions of the License, a copy of which |
| 10 | * is available by writing to the Free Software Foundation, Inc., |
| 11 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the |
| 12 | * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. |
| 13 | * |
| 14 | * |
| 15 | * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE EXPRESSLY DISCLAIMED. The License provides additional details about |
| 18 | * this warranty disclaimer. |
| 19 | **/ |
| 20 | |
Paul Gortmaker | a59b968 | 2011-08-29 16:44:23 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 23 | #include <net/bluetooth/bluetooth.h> |
| 24 | #include <net/bluetooth/hci_core.h> |
| 25 | |
| 26 | #include "btmrvl_drv.h" |
| 27 | |
| 28 | #define VERSION "1.0" |
| 29 | |
| 30 | /* |
| 31 | * This function is called by interface specific interrupt handler. |
| 32 | * It updates Power Save & Host Sleep states, and wakes up the main |
| 33 | * thread. |
| 34 | */ |
| 35 | void btmrvl_interrupt(struct btmrvl_private *priv) |
| 36 | { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 37 | priv->adapter->ps_state = PS_AWAKE; |
| 38 | |
| 39 | priv->adapter->wakeup_tries = 0; |
| 40 | |
| 41 | priv->adapter->int_count++; |
| 42 | |
| 43 | wake_up_interruptible(&priv->main_thread.wait_q); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 44 | } |
| 45 | EXPORT_SYMBOL_GPL(btmrvl_interrupt); |
| 46 | |
| 47 | void btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb) |
| 48 | { |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 49 | struct hci_event_hdr *hdr = (void *) skb->data; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 50 | struct hci_ev_cmd_complete *ec; |
| 51 | u16 opcode, ocf; |
| 52 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 53 | if (hdr->evt == HCI_EV_CMD_COMPLETE) { |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 54 | ec = (void *) (skb->data + HCI_EVENT_HDR_SIZE); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 55 | opcode = __le16_to_cpu(ec->opcode); |
| 56 | ocf = hci_opcode_ocf(opcode); |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 57 | if (ocf == BT_CMD_MODULE_CFG_REQ && |
| 58 | priv->btmrvl_dev.sendcmdflag) { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 59 | priv->btmrvl_dev.sendcmdflag = false; |
| 60 | priv->adapter->cmd_complete = true; |
| 61 | wake_up_interruptible(&priv->adapter->cmd_wait_q); |
| 62 | } |
| 63 | } |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 64 | } |
| 65 | EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt); |
| 66 | |
| 67 | int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb) |
| 68 | { |
| 69 | struct btmrvl_adapter *adapter = priv->adapter; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 70 | struct btmrvl_event *event; |
Dan Carpenter | 31f085f | 2010-03-08 15:15:59 +0300 | [diff] [blame] | 71 | int ret = 0; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 72 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 73 | event = (struct btmrvl_event *) skb->data; |
| 74 | if (event->ec != 0xff) { |
| 75 | BT_DBG("Not Marvell Event=%x", event->ec); |
| 76 | ret = -EINVAL; |
| 77 | goto exit; |
| 78 | } |
| 79 | |
| 80 | switch (event->data[0]) { |
| 81 | case BT_CMD_AUTO_SLEEP_MODE: |
| 82 | if (!event->data[2]) { |
| 83 | if (event->data[1] == BT_PS_ENABLE) |
| 84 | adapter->psmode = 1; |
| 85 | else |
| 86 | adapter->psmode = 0; |
| 87 | BT_DBG("PS Mode:%s", |
| 88 | (adapter->psmode) ? "Enable" : "Disable"); |
| 89 | } else { |
| 90 | BT_DBG("PS Mode command failed"); |
| 91 | } |
| 92 | break; |
| 93 | |
| 94 | case BT_CMD_HOST_SLEEP_CONFIG: |
| 95 | if (!event->data[3]) |
| 96 | BT_DBG("gpio=%x, gap=%x", event->data[1], |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 97 | event->data[2]); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 98 | else |
| 99 | BT_DBG("HSCFG command failed"); |
| 100 | break; |
| 101 | |
| 102 | case BT_CMD_HOST_SLEEP_ENABLE: |
| 103 | if (!event->data[1]) { |
| 104 | adapter->hs_state = HS_ACTIVATED; |
| 105 | if (adapter->psmode) |
| 106 | adapter->ps_state = PS_SLEEP; |
| 107 | wake_up_interruptible(&adapter->cmd_wait_q); |
| 108 | BT_DBG("HS ACTIVATED!"); |
| 109 | } else { |
| 110 | BT_DBG("HS Enable failed"); |
| 111 | } |
| 112 | break; |
| 113 | |
| 114 | case BT_CMD_MODULE_CFG_REQ: |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 115 | if (priv->btmrvl_dev.sendcmdflag && |
| 116 | event->data[1] == MODULE_BRINGUP_REQ) { |
Bing Zhao | 903c843 | 2010-03-03 14:37:35 -0800 | [diff] [blame] | 117 | BT_DBG("EVENT:%s", |
| 118 | ((event->data[2] == MODULE_BROUGHT_UP) || |
| 119 | (event->data[2] == MODULE_ALREADY_UP)) ? |
| 120 | "Bring-up succeed" : "Bring-up failed"); |
Bing Zhao | f120c6b | 2010-03-03 14:37:37 -0800 | [diff] [blame] | 121 | |
David Vrabel | 8f1e174 | 2010-08-09 17:38:10 -0400 | [diff] [blame] | 122 | if (event->length > 3 && event->data[3]) |
| 123 | priv->btmrvl_dev.dev_type = HCI_AMP; |
Bing Zhao | f120c6b | 2010-03-03 14:37:37 -0800 | [diff] [blame] | 124 | else |
| 125 | priv->btmrvl_dev.dev_type = HCI_BREDR; |
| 126 | |
| 127 | BT_DBG("dev_type: %d", priv->btmrvl_dev.dev_type); |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 128 | } else if (priv->btmrvl_dev.sendcmdflag && |
| 129 | event->data[1] == MODULE_SHUTDOWN_REQ) { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 130 | BT_DBG("EVENT:%s", (event->data[2]) ? |
| 131 | "Shutdown failed" : "Shutdown succeed"); |
| 132 | } else { |
| 133 | BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP"); |
| 134 | ret = -EINVAL; |
| 135 | } |
| 136 | break; |
| 137 | |
| 138 | case BT_EVENT_POWER_STATE: |
| 139 | if (event->data[1] == BT_PS_SLEEP) |
| 140 | adapter->ps_state = PS_SLEEP; |
| 141 | BT_DBG("EVENT:%s", |
| 142 | (adapter->ps_state) ? "PS_SLEEP" : "PS_AWAKE"); |
| 143 | break; |
| 144 | |
| 145 | default: |
| 146 | BT_DBG("Unknown Event=%d", event->data[0]); |
| 147 | ret = -EINVAL; |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | exit: |
| 152 | if (!ret) |
| 153 | kfree_skb(skb); |
| 154 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(btmrvl_process_event); |
| 158 | |
| 159 | int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, int subcmd) |
| 160 | { |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 161 | struct sk_buff *skb; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 162 | struct btmrvl_cmd *cmd; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 163 | int ret = 0; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 164 | |
| 165 | skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC); |
| 166 | if (skb == NULL) { |
| 167 | BT_ERR("No free skb"); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 168 | return -ENOMEM; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Marcel Holtmann | e0721f9 | 2009-06-13 07:27:19 +0200 | [diff] [blame] | 171 | cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd)); |
Marcel Holtmann | 91d6973 | 2009-06-10 12:18:50 +0200 | [diff] [blame] | 172 | cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_MODULE_CFG_REQ)); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 173 | cmd->length = 1; |
| 174 | cmd->data[0] = subcmd; |
| 175 | |
| 176 | bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT; |
| 177 | |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 178 | skb->dev = (void *) priv->btmrvl_dev.hcidev; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 179 | skb_queue_head(&priv->adapter->tx_queue, skb); |
| 180 | |
| 181 | priv->btmrvl_dev.sendcmdflag = true; |
| 182 | |
| 183 | priv->adapter->cmd_complete = false; |
| 184 | |
| 185 | BT_DBG("Queue module cfg Command"); |
| 186 | |
| 187 | wake_up_interruptible(&priv->main_thread.wait_q); |
| 188 | |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 189 | if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q, |
| 190 | priv->adapter->cmd_complete, |
| 191 | msecs_to_jiffies(WAIT_UNTIL_CMD_RESP))) { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 192 | ret = -ETIMEDOUT; |
| 193 | BT_ERR("module_cfg_cmd(%x): timeout: %d", |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 194 | subcmd, priv->btmrvl_dev.sendcmdflag); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | BT_DBG("module cfg Command done"); |
| 198 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 199 | return ret; |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd); |
| 202 | |
Amitkumar Karwar | 2a8ff61 | 2012-04-24 16:31:40 -0700 | [diff] [blame] | 203 | int btmrvl_send_hscfg_cmd(struct btmrvl_private *priv) |
| 204 | { |
| 205 | struct sk_buff *skb; |
| 206 | struct btmrvl_cmd *cmd; |
| 207 | |
| 208 | skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC); |
| 209 | if (!skb) { |
| 210 | BT_ERR("No free skb"); |
| 211 | return -ENOMEM; |
| 212 | } |
| 213 | |
| 214 | cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd)); |
| 215 | cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, |
| 216 | BT_CMD_HOST_SLEEP_CONFIG)); |
| 217 | cmd->length = 2; |
| 218 | cmd->data[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8; |
| 219 | cmd->data[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff); |
| 220 | |
| 221 | bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT; |
| 222 | |
| 223 | skb->dev = (void *) priv->btmrvl_dev.hcidev; |
| 224 | skb_queue_head(&priv->adapter->tx_queue, skb); |
| 225 | |
| 226 | BT_DBG("Queue HSCFG Command, gpio=0x%x, gap=0x%x", cmd->data[0], |
| 227 | cmd->data[1]); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd); |
| 232 | |
Amitkumar Karwar | 32fd639 | 2009-09-30 11:18:30 -0700 | [diff] [blame] | 233 | int btmrvl_enable_ps(struct btmrvl_private *priv) |
| 234 | { |
| 235 | struct sk_buff *skb; |
| 236 | struct btmrvl_cmd *cmd; |
| 237 | |
| 238 | skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC); |
| 239 | if (skb == NULL) { |
| 240 | BT_ERR("No free skb"); |
| 241 | return -ENOMEM; |
| 242 | } |
| 243 | |
| 244 | cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd)); |
| 245 | cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, |
| 246 | BT_CMD_AUTO_SLEEP_MODE)); |
| 247 | cmd->length = 1; |
| 248 | |
| 249 | if (priv->btmrvl_dev.psmode) |
| 250 | cmd->data[0] = BT_PS_ENABLE; |
| 251 | else |
| 252 | cmd->data[0] = BT_PS_DISABLE; |
| 253 | |
| 254 | bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT; |
| 255 | |
| 256 | skb->dev = (void *) priv->btmrvl_dev.hcidev; |
| 257 | skb_queue_head(&priv->adapter->tx_queue, skb); |
| 258 | |
| 259 | BT_DBG("Queue PSMODE Command:%d", cmd->data[0]); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | EXPORT_SYMBOL_GPL(btmrvl_enable_ps); |
| 264 | |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 265 | int btmrvl_enable_hs(struct btmrvl_private *priv) |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 266 | { |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 267 | struct sk_buff *skb; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 268 | struct btmrvl_cmd *cmd; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 269 | int ret = 0; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 270 | |
| 271 | skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC); |
| 272 | if (skb == NULL) { |
| 273 | BT_ERR("No free skb"); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 274 | return -ENOMEM; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Marcel Holtmann | e0721f9 | 2009-06-13 07:27:19 +0200 | [diff] [blame] | 277 | cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd)); |
Marcel Holtmann | 91d6973 | 2009-06-10 12:18:50 +0200 | [diff] [blame] | 278 | cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_HOST_SLEEP_ENABLE)); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 279 | cmd->length = 0; |
| 280 | |
| 281 | bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT; |
| 282 | |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 283 | skb->dev = (void *) priv->btmrvl_dev.hcidev; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 284 | skb_queue_head(&priv->adapter->tx_queue, skb); |
| 285 | |
| 286 | BT_DBG("Queue hs enable Command"); |
| 287 | |
| 288 | wake_up_interruptible(&priv->main_thread.wait_q); |
| 289 | |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 290 | if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q, |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 291 | priv->adapter->hs_state, |
| 292 | msecs_to_jiffies(WAIT_UNTIL_HS_STATE_CHANGED))) { |
| 293 | ret = -ETIMEDOUT; |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 294 | BT_ERR("timeout: %d, %d,%d", priv->adapter->hs_state, |
| 295 | priv->adapter->ps_state, |
| 296 | priv->adapter->wakeup_tries); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 299 | return ret; |
| 300 | } |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 301 | EXPORT_SYMBOL_GPL(btmrvl_enable_hs); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 302 | |
| 303 | int btmrvl_prepare_command(struct btmrvl_private *priv) |
| 304 | { |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 305 | int ret = 0; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 306 | |
| 307 | if (priv->btmrvl_dev.hscfgcmd) { |
| 308 | priv->btmrvl_dev.hscfgcmd = 0; |
Amitkumar Karwar | 2a8ff61 | 2012-04-24 16:31:40 -0700 | [diff] [blame] | 309 | btmrvl_send_hscfg_cmd(priv); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (priv->btmrvl_dev.pscmd) { |
| 313 | priv->btmrvl_dev.pscmd = 0; |
Amitkumar Karwar | 32fd639 | 2009-09-30 11:18:30 -0700 | [diff] [blame] | 314 | btmrvl_enable_ps(priv); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | if (priv->btmrvl_dev.hscmd) { |
| 318 | priv->btmrvl_dev.hscmd = 0; |
| 319 | |
| 320 | if (priv->btmrvl_dev.hsmode) { |
| 321 | ret = btmrvl_enable_hs(priv); |
| 322 | } else { |
| 323 | ret = priv->hw_wakeup_firmware(priv); |
| 324 | priv->adapter->hs_state = HS_DEACTIVATED; |
| 325 | } |
| 326 | } |
| 327 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb) |
| 332 | { |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 333 | int ret = 0; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 334 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 335 | if (!skb || !skb->data) |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 336 | return -EINVAL; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 337 | |
| 338 | if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) { |
| 339 | BT_ERR("Tx Error: Bad skb length %d : %d", |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 340 | skb->len, BTM_UPLD_SIZE); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 341 | return -EINVAL; |
| 342 | } |
| 343 | |
| 344 | if (skb_headroom(skb) < BTM_HEADER_LEN) { |
| 345 | struct sk_buff *tmp = skb; |
| 346 | |
| 347 | skb = skb_realloc_headroom(skb, BTM_HEADER_LEN); |
| 348 | if (!skb) { |
| 349 | BT_ERR("Tx Error: realloc_headroom failed %d", |
| 350 | BTM_HEADER_LEN); |
| 351 | skb = tmp; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 352 | return -EINVAL; |
| 353 | } |
| 354 | |
| 355 | kfree_skb(tmp); |
| 356 | } |
| 357 | |
| 358 | skb_push(skb, BTM_HEADER_LEN); |
| 359 | |
| 360 | /* header type: byte[3] |
| 361 | * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor |
| 362 | * header length: byte[2][1][0] |
| 363 | */ |
| 364 | |
| 365 | skb->data[0] = (skb->len & 0x0000ff); |
| 366 | skb->data[1] = (skb->len & 0x00ff00) >> 8; |
| 367 | skb->data[2] = (skb->len & 0xff0000) >> 16; |
| 368 | skb->data[3] = bt_cb(skb)->pkt_type; |
| 369 | |
| 370 | if (priv->hw_host_to_card) |
| 371 | ret = priv->hw_host_to_card(priv, skb->data, skb->len); |
| 372 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | static void btmrvl_init_adapter(struct btmrvl_private *priv) |
| 377 | { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 378 | skb_queue_head_init(&priv->adapter->tx_queue); |
| 379 | |
| 380 | priv->adapter->ps_state = PS_AWAKE; |
| 381 | |
| 382 | init_waitqueue_head(&priv->adapter->cmd_wait_q); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static void btmrvl_free_adapter(struct btmrvl_private *priv) |
| 386 | { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 387 | skb_queue_purge(&priv->adapter->tx_queue); |
| 388 | |
| 389 | kfree(priv->adapter); |
| 390 | |
| 391 | priv->adapter = NULL; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 394 | static int btmrvl_ioctl(struct hci_dev *hdev, |
| 395 | unsigned int cmd, unsigned long arg) |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 396 | { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 397 | return -ENOIOCTLCMD; |
| 398 | } |
| 399 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 400 | static int btmrvl_send_frame(struct sk_buff *skb) |
| 401 | { |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 402 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 403 | struct btmrvl_private *priv = NULL; |
| 404 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 405 | BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 406 | |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 407 | if (!hdev) { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 408 | BT_ERR("Frame for unknown HCI device"); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 409 | return -ENODEV; |
| 410 | } |
| 411 | |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 412 | priv = hci_get_drvdata(hdev); |
| 413 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 414 | if (!test_bit(HCI_RUNNING, &hdev->flags)) { |
| 415 | BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev->flags); |
| 416 | print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET, |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 417 | skb->data, skb->len); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 418 | return -EBUSY; |
| 419 | } |
| 420 | |
| 421 | switch (bt_cb(skb)->pkt_type) { |
| 422 | case HCI_COMMAND_PKT: |
| 423 | hdev->stat.cmd_tx++; |
| 424 | break; |
| 425 | |
| 426 | case HCI_ACLDATA_PKT: |
| 427 | hdev->stat.acl_tx++; |
| 428 | break; |
| 429 | |
| 430 | case HCI_SCODATA_PKT: |
| 431 | hdev->stat.sco_tx++; |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | skb_queue_tail(&priv->adapter->tx_queue, skb); |
| 436 | |
| 437 | wake_up_interruptible(&priv->main_thread.wait_q); |
| 438 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int btmrvl_flush(struct hci_dev *hdev) |
| 443 | { |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 444 | struct btmrvl_private *priv = hci_get_drvdata(hdev); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 445 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 446 | skb_queue_purge(&priv->adapter->tx_queue); |
| 447 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static int btmrvl_close(struct hci_dev *hdev) |
| 452 | { |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 453 | struct btmrvl_private *priv = hci_get_drvdata(hdev); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 454 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 455 | if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 456 | return 0; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 457 | |
| 458 | skb_queue_purge(&priv->adapter->tx_queue); |
| 459 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static int btmrvl_open(struct hci_dev *hdev) |
| 464 | { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 465 | set_bit(HCI_RUNNING, &hdev->flags); |
| 466 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * This function handles the event generated by firmware, rx data |
| 472 | * received from firmware, and tx data sent from kernel. |
| 473 | */ |
| 474 | static int btmrvl_service_main_thread(void *data) |
| 475 | { |
| 476 | struct btmrvl_thread *thread = data; |
| 477 | struct btmrvl_private *priv = thread->priv; |
| 478 | struct btmrvl_adapter *adapter = priv->adapter; |
| 479 | wait_queue_t wait; |
| 480 | struct sk_buff *skb; |
| 481 | ulong flags; |
| 482 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 483 | init_waitqueue_entry(&wait, current); |
| 484 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 485 | for (;;) { |
| 486 | add_wait_queue(&thread->wait_q, &wait); |
| 487 | |
| 488 | set_current_state(TASK_INTERRUPTIBLE); |
| 489 | |
| 490 | if (adapter->wakeup_tries || |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 491 | ((!adapter->int_count) && |
| 492 | (!priv->btmrvl_dev.tx_dnld_rdy || |
| 493 | skb_queue_empty(&adapter->tx_queue)))) { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 494 | BT_DBG("main_thread is sleeping..."); |
| 495 | schedule(); |
| 496 | } |
| 497 | |
| 498 | set_current_state(TASK_RUNNING); |
| 499 | |
| 500 | remove_wait_queue(&thread->wait_q, &wait); |
| 501 | |
| 502 | BT_DBG("main_thread woke up"); |
| 503 | |
| 504 | if (kthread_should_stop()) { |
| 505 | BT_DBG("main_thread: break from main thread"); |
| 506 | break; |
| 507 | } |
| 508 | |
| 509 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 510 | if (adapter->int_count) { |
| 511 | adapter->int_count = 0; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 512 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 513 | priv->hw_process_int_status(priv); |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 514 | } else if (adapter->ps_state == PS_SLEEP && |
| 515 | !skb_queue_empty(&adapter->tx_queue)) { |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 516 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 517 | adapter->wakeup_tries++; |
| 518 | priv->hw_wakeup_firmware(priv); |
| 519 | continue; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 520 | } else { |
| 521 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 522 | } |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 523 | |
| 524 | if (adapter->ps_state == PS_SLEEP) |
| 525 | continue; |
| 526 | |
| 527 | if (!priv->btmrvl_dev.tx_dnld_rdy) |
| 528 | continue; |
| 529 | |
| 530 | skb = skb_dequeue(&adapter->tx_queue); |
| 531 | if (skb) { |
| 532 | if (btmrvl_tx_pkt(priv, skb)) |
| 533 | priv->btmrvl_dev.hcidev->stat.err_tx++; |
| 534 | else |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 535 | priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 536 | |
| 537 | kfree_skb(skb); |
| 538 | } |
| 539 | } |
| 540 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 544 | int btmrvl_register_hdev(struct btmrvl_private *priv) |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 545 | { |
| 546 | struct hci_dev *hdev = NULL; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 547 | int ret; |
| 548 | |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 549 | hdev = hci_alloc_dev(); |
| 550 | if (!hdev) { |
| 551 | BT_ERR("Can not allocate HCI device"); |
| 552 | goto err_hdev; |
| 553 | } |
| 554 | |
| 555 | priv->btmrvl_dev.hcidev = hdev; |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 556 | hci_set_drvdata(hdev, priv); |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 557 | |
| 558 | hdev->bus = HCI_SDIO; |
| 559 | hdev->open = btmrvl_open; |
| 560 | hdev->close = btmrvl_close; |
| 561 | hdev->flush = btmrvl_flush; |
| 562 | hdev->send = btmrvl_send_frame; |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 563 | hdev->ioctl = btmrvl_ioctl; |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 564 | |
| 565 | btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ); |
| 566 | |
Bing Zhao | f120c6b | 2010-03-03 14:37:37 -0800 | [diff] [blame] | 567 | hdev->dev_type = priv->btmrvl_dev.dev_type; |
| 568 | |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 569 | ret = hci_register_dev(hdev); |
| 570 | if (ret < 0) { |
| 571 | BT_ERR("Can not register HCI device"); |
| 572 | goto err_hci_register_dev; |
| 573 | } |
| 574 | |
| 575 | #ifdef CONFIG_DEBUG_FS |
| 576 | btmrvl_debugfs_init(hdev); |
| 577 | #endif |
| 578 | |
| 579 | return 0; |
| 580 | |
| 581 | err_hci_register_dev: |
| 582 | hci_free_dev(hdev); |
| 583 | |
| 584 | err_hdev: |
| 585 | /* Stop the thread servicing the interrupts */ |
| 586 | kthread_stop(priv->main_thread.task); |
| 587 | |
| 588 | btmrvl_free_adapter(priv); |
| 589 | kfree(priv); |
| 590 | |
| 591 | return -ENOMEM; |
| 592 | } |
| 593 | EXPORT_SYMBOL_GPL(btmrvl_register_hdev); |
| 594 | |
| 595 | struct btmrvl_private *btmrvl_add_card(void *card) |
| 596 | { |
| 597 | struct btmrvl_private *priv; |
| 598 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 599 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 600 | if (!priv) { |
| 601 | BT_ERR("Can not allocate priv"); |
| 602 | goto err_priv; |
| 603 | } |
| 604 | |
| 605 | priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL); |
| 606 | if (!priv->adapter) { |
| 607 | BT_ERR("Allocate buffer for btmrvl_adapter failed!"); |
| 608 | goto err_adapter; |
| 609 | } |
| 610 | |
| 611 | btmrvl_init_adapter(priv); |
| 612 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 613 | BT_DBG("Starting kthread..."); |
| 614 | priv->main_thread.priv = priv; |
| 615 | spin_lock_init(&priv->driver_lock); |
| 616 | |
| 617 | init_waitqueue_head(&priv->main_thread.wait_q); |
| 618 | priv->main_thread.task = kthread_run(btmrvl_service_main_thread, |
Marcel Holtmann | 4271e08 | 2009-06-09 16:00:22 +0200 | [diff] [blame] | 619 | &priv->main_thread, "btmrvl_main_service"); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 620 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 621 | priv->btmrvl_dev.card = card; |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 622 | priv->btmrvl_dev.tx_dnld_rdy = true; |
| 623 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 624 | return priv; |
| 625 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 626 | err_adapter: |
| 627 | kfree(priv); |
| 628 | |
| 629 | err_priv: |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 630 | return NULL; |
| 631 | } |
| 632 | EXPORT_SYMBOL_GPL(btmrvl_add_card); |
| 633 | |
| 634 | int btmrvl_remove_card(struct btmrvl_private *priv) |
| 635 | { |
| 636 | struct hci_dev *hdev; |
| 637 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 638 | hdev = priv->btmrvl_dev.hcidev; |
| 639 | |
| 640 | wake_up_interruptible(&priv->adapter->cmd_wait_q); |
| 641 | |
| 642 | kthread_stop(priv->main_thread.task); |
| 643 | |
Bing Zhao | fb784f0 | 2009-06-02 14:29:37 -0700 | [diff] [blame] | 644 | #ifdef CONFIG_DEBUG_FS |
| 645 | btmrvl_debugfs_remove(hdev); |
| 646 | #endif |
| 647 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 648 | hci_unregister_dev(hdev); |
| 649 | |
| 650 | hci_free_dev(hdev); |
| 651 | |
| 652 | priv->btmrvl_dev.hcidev = NULL; |
| 653 | |
| 654 | btmrvl_free_adapter(priv); |
| 655 | |
| 656 | kfree(priv); |
| 657 | |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | EXPORT_SYMBOL_GPL(btmrvl_remove_card); |
| 661 | |
| 662 | MODULE_AUTHOR("Marvell International Ltd."); |
Marcel Holtmann | 9666fb3 | 2009-06-09 21:45:04 +0200 | [diff] [blame] | 663 | MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION); |
Bing Zhao | 132ff4e | 2009-06-02 14:29:35 -0700 | [diff] [blame] | 664 | MODULE_VERSION(VERSION); |
| 665 | MODULE_LICENSE("GPL v2"); |