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