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