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