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