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