btmrvl_main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <net/bluetooth/bluetooth.h>
  23. #include <net/bluetooth/hci_core.h>
  24. #include <linux/mmc/sdio_func.h>
  25. #include "btmrvl_drv.h"
  26. #include "btmrvl_sdio.h"
  27. #define VERSION "1.0"
  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. priv->adapter->ps_state = PS_AWAKE;
  36. priv->adapter->wakeup_tries = 0;
  37. priv->adapter->int_count++;
  38. if (priv->adapter->hs_state == HS_ACTIVATED) {
  39. BT_DBG("BT: HS DEACTIVATED in ISR!");
  40. priv->adapter->hs_state = HS_DEACTIVATED;
  41. }
  42. wake_up_interruptible(&priv->main_thread.wait_q);
  43. }
  44. EXPORT_SYMBOL_GPL(btmrvl_interrupt);
  45. bool btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb)
  46. {
  47. struct hci_event_hdr *hdr = (void *) skb->data;
  48. if (hdr->evt == HCI_EV_CMD_COMPLETE) {
  49. struct hci_ev_cmd_complete *ec;
  50. u16 opcode;
  51. ec = (void *) (skb->data + HCI_EVENT_HDR_SIZE);
  52. opcode = __le16_to_cpu(ec->opcode);
  53. if (priv->btmrvl_dev.sendcmdflag) {
  54. priv->btmrvl_dev.sendcmdflag = false;
  55. priv->adapter->cmd_complete = true;
  56. wake_up_interruptible(&priv->adapter->cmd_wait_q);
  57. if (hci_opcode_ogf(opcode) == 0x3F) {
  58. BT_DBG("vendor event skipped: opcode=%#4.4x",
  59. opcode);
  60. kfree_skb(skb);
  61. return false;
  62. }
  63. }
  64. }
  65. return true;
  66. }
  67. EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt);
  68. int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb)
  69. {
  70. struct btmrvl_adapter *adapter = priv->adapter;
  71. struct btmrvl_event *event;
  72. int ret = 0;
  73. event = (struct btmrvl_event *) skb->data;
  74. if (event->ec != 0xff) {
  75. BT_DBG("Not Marvell Event=%x", event->ec);
  76. ret = -EINVAL;
  77. goto exit;
  78. }
  79. switch (event->data[0]) {
  80. case BT_EVENT_AUTO_SLEEP_MODE:
  81. if (!event->data[2]) {
  82. if (event->data[1] == BT_PS_ENABLE)
  83. adapter->psmode = 1;
  84. else
  85. adapter->psmode = 0;
  86. BT_DBG("PS Mode:%s",
  87. (adapter->psmode) ? "Enable" : "Disable");
  88. } else {
  89. BT_DBG("PS Mode command failed");
  90. }
  91. break;
  92. case BT_EVENT_HOST_SLEEP_CONFIG:
  93. if (!event->data[3])
  94. BT_DBG("gpio=%x, gap=%x", event->data[1],
  95. event->data[2]);
  96. else
  97. BT_DBG("HSCFG command failed");
  98. break;
  99. case BT_EVENT_HOST_SLEEP_ENABLE:
  100. if (!event->data[1]) {
  101. adapter->hs_state = HS_ACTIVATED;
  102. if (adapter->psmode)
  103. adapter->ps_state = PS_SLEEP;
  104. wake_up_interruptible(&adapter->event_hs_wait_q);
  105. BT_DBG("HS ACTIVATED!");
  106. } else {
  107. BT_DBG("HS Enable failed");
  108. }
  109. break;
  110. case BT_EVENT_MODULE_CFG_REQ:
  111. if (priv->btmrvl_dev.sendcmdflag &&
  112. event->data[1] == MODULE_BRINGUP_REQ) {
  113. BT_DBG("EVENT:%s",
  114. ((event->data[2] == MODULE_BROUGHT_UP) ||
  115. (event->data[2] == MODULE_ALREADY_UP)) ?
  116. "Bring-up succeed" : "Bring-up failed");
  117. if (event->length > 3 && event->data[3])
  118. priv->btmrvl_dev.dev_type = HCI_AMP;
  119. else
  120. priv->btmrvl_dev.dev_type = HCI_BREDR;
  121. BT_DBG("dev_type: %d", priv->btmrvl_dev.dev_type);
  122. } else if (priv->btmrvl_dev.sendcmdflag &&
  123. event->data[1] == MODULE_SHUTDOWN_REQ) {
  124. BT_DBG("EVENT:%s", (event->data[2]) ?
  125. "Shutdown failed" : "Shutdown succeed");
  126. } else {
  127. BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
  128. ret = -EINVAL;
  129. }
  130. break;
  131. case BT_EVENT_POWER_STATE:
  132. if (event->data[1] == BT_PS_SLEEP)
  133. adapter->ps_state = PS_SLEEP;
  134. BT_DBG("EVENT:%s",
  135. (adapter->ps_state) ? "PS_SLEEP" : "PS_AWAKE");
  136. break;
  137. default:
  138. BT_DBG("Unknown Event=%d", event->data[0]);
  139. ret = -EINVAL;
  140. break;
  141. }
  142. exit:
  143. if (!ret)
  144. kfree_skb(skb);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(btmrvl_process_event);
  148. static int btmrvl_send_sync_cmd(struct btmrvl_private *priv, u16 opcode,
  149. const void *param, u8 len)
  150. {
  151. struct sk_buff *skb;
  152. struct hci_command_hdr *hdr;
  153. if (priv->surprise_removed) {
  154. BT_ERR("Card is removed");
  155. return -EFAULT;
  156. }
  157. skb = bt_skb_alloc(HCI_COMMAND_HDR_SIZE + len, GFP_ATOMIC);
  158. if (!skb) {
  159. BT_ERR("No free skb");
  160. return -ENOMEM;
  161. }
  162. hdr = (struct hci_command_hdr *)skb_put(skb, HCI_COMMAND_HDR_SIZE);
  163. hdr->opcode = cpu_to_le16(opcode);
  164. hdr->plen = len;
  165. if (len)
  166. memcpy(skb_put(skb, len), param, len);
  167. bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
  168. skb_queue_head(&priv->adapter->tx_queue, skb);
  169. priv->btmrvl_dev.sendcmdflag = true;
  170. priv->adapter->cmd_complete = false;
  171. wake_up_interruptible(&priv->main_thread.wait_q);
  172. if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
  173. priv->adapter->cmd_complete ||
  174. priv->surprise_removed,
  175. WAIT_UNTIL_CMD_RESP))
  176. return -ETIMEDOUT;
  177. if (priv->surprise_removed)
  178. return -EFAULT;
  179. return 0;
  180. }
  181. int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, u8 subcmd)
  182. {
  183. int ret;
  184. ret = btmrvl_send_sync_cmd(priv, BT_CMD_MODULE_CFG_REQ, &subcmd, 1);
  185. if (ret)
  186. BT_ERR("module_cfg_cmd(%x) failed", subcmd);
  187. return ret;
  188. }
  189. EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd);
  190. static int btmrvl_enable_sco_routing_to_host(struct btmrvl_private *priv)
  191. {
  192. int ret;
  193. u8 subcmd = 0;
  194. ret = btmrvl_send_sync_cmd(priv, BT_CMD_ROUTE_SCO_TO_HOST, &subcmd, 1);
  195. if (ret)
  196. BT_ERR("BT_CMD_ROUTE_SCO_TO_HOST command failed: %#x", ret);
  197. return ret;
  198. }
  199. int btmrvl_pscan_window_reporting(struct btmrvl_private *priv, u8 subcmd)
  200. {
  201. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  202. int ret;
  203. if (!card->support_pscan_win_report)
  204. return 0;
  205. ret = btmrvl_send_sync_cmd(priv, BT_CMD_PSCAN_WIN_REPORT_ENABLE,
  206. &subcmd, 1);
  207. if (ret)
  208. BT_ERR("PSCAN_WIN_REPORT_ENABLE command failed: %#x", ret);
  209. return ret;
  210. }
  211. EXPORT_SYMBOL_GPL(btmrvl_pscan_window_reporting);
  212. int btmrvl_send_hscfg_cmd(struct btmrvl_private *priv)
  213. {
  214. int ret;
  215. u8 param[2];
  216. param[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
  217. param[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff);
  218. BT_DBG("Sending HSCFG Command, gpio=0x%x, gap=0x%x",
  219. param[0], param[1]);
  220. ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_CONFIG, param, 2);
  221. if (ret)
  222. BT_ERR("HSCFG command failed");
  223. return ret;
  224. }
  225. EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd);
  226. int btmrvl_enable_ps(struct btmrvl_private *priv)
  227. {
  228. int ret;
  229. u8 param;
  230. if (priv->btmrvl_dev.psmode)
  231. param = BT_PS_ENABLE;
  232. else
  233. param = BT_PS_DISABLE;
  234. ret = btmrvl_send_sync_cmd(priv, BT_CMD_AUTO_SLEEP_MODE, &param, 1);
  235. if (ret)
  236. BT_ERR("PSMODE command failed");
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(btmrvl_enable_ps);
  240. int btmrvl_enable_hs(struct btmrvl_private *priv)
  241. {
  242. struct btmrvl_adapter *adapter = priv->adapter;
  243. int ret;
  244. ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_ENABLE, NULL, 0);
  245. if (ret) {
  246. BT_ERR("Host sleep enable command failed");
  247. return ret;
  248. }
  249. ret = wait_event_interruptible_timeout(adapter->event_hs_wait_q,
  250. adapter->hs_state ||
  251. priv->surprise_removed,
  252. WAIT_UNTIL_HS_STATE_CHANGED);
  253. if (ret < 0 || priv->surprise_removed) {
  254. BT_ERR("event_hs_wait_q terminated (%d): %d,%d,%d",
  255. ret, adapter->hs_state, adapter->ps_state,
  256. adapter->wakeup_tries);
  257. } else if (!ret) {
  258. BT_ERR("hs_enable timeout: %d,%d,%d", adapter->hs_state,
  259. adapter->ps_state, adapter->wakeup_tries);
  260. ret = -ETIMEDOUT;
  261. } else {
  262. BT_DBG("host sleep enabled: %d,%d,%d", adapter->hs_state,
  263. adapter->ps_state, adapter->wakeup_tries);
  264. ret = 0;
  265. }
  266. return ret;
  267. }
  268. EXPORT_SYMBOL_GPL(btmrvl_enable_hs);
  269. int btmrvl_prepare_command(struct btmrvl_private *priv)
  270. {
  271. int ret = 0;
  272. if (priv->btmrvl_dev.hscfgcmd) {
  273. priv->btmrvl_dev.hscfgcmd = 0;
  274. btmrvl_send_hscfg_cmd(priv);
  275. }
  276. if (priv->btmrvl_dev.pscmd) {
  277. priv->btmrvl_dev.pscmd = 0;
  278. btmrvl_enable_ps(priv);
  279. }
  280. if (priv->btmrvl_dev.hscmd) {
  281. priv->btmrvl_dev.hscmd = 0;
  282. if (priv->btmrvl_dev.hsmode) {
  283. ret = btmrvl_enable_hs(priv);
  284. } else {
  285. ret = priv->hw_wakeup_firmware(priv);
  286. priv->adapter->hs_state = HS_DEACTIVATED;
  287. BT_DBG("BT: HS DEACTIVATED due to host activity!");
  288. }
  289. }
  290. return ret;
  291. }
  292. void btmrvl_firmware_dump(struct btmrvl_private *priv)
  293. {
  294. if (priv->firmware_dump)
  295. priv->firmware_dump(priv);
  296. }
  297. static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
  298. {
  299. int ret = 0;
  300. if (!skb || !skb->data)
  301. return -EINVAL;
  302. if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
  303. BT_ERR("Tx Error: Bad skb length %d : %d",
  304. skb->len, BTM_UPLD_SIZE);
  305. return -EINVAL;
  306. }
  307. skb_push(skb, BTM_HEADER_LEN);
  308. /* header type: byte[3]
  309. * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
  310. * header length: byte[2][1][0]
  311. */
  312. skb->data[0] = (skb->len & 0x0000ff);
  313. skb->data[1] = (skb->len & 0x00ff00) >> 8;
  314. skb->data[2] = (skb->len & 0xff0000) >> 16;
  315. skb->data[3] = bt_cb(skb)->pkt_type;
  316. if (priv->hw_host_to_card)
  317. ret = priv->hw_host_to_card(priv, skb->data, skb->len);
  318. return ret;
  319. }
  320. static void btmrvl_init_adapter(struct btmrvl_private *priv)
  321. {
  322. int buf_size;
  323. skb_queue_head_init(&priv->adapter->tx_queue);
  324. priv->adapter->ps_state = PS_AWAKE;
  325. buf_size = ALIGN_SZ(SDIO_BLOCK_SIZE, BTSDIO_DMA_ALIGN);
  326. priv->adapter->hw_regs_buf = kzalloc(buf_size, GFP_KERNEL);
  327. if (!priv->adapter->hw_regs_buf) {
  328. priv->adapter->hw_regs = NULL;
  329. BT_ERR("Unable to allocate buffer for hw_regs.");
  330. } else {
  331. priv->adapter->hw_regs =
  332. (u8 *)ALIGN_ADDR(priv->adapter->hw_regs_buf,
  333. BTSDIO_DMA_ALIGN);
  334. BT_DBG("hw_regs_buf=%p hw_regs=%p",
  335. priv->adapter->hw_regs_buf, priv->adapter->hw_regs);
  336. }
  337. init_waitqueue_head(&priv->adapter->cmd_wait_q);
  338. init_waitqueue_head(&priv->adapter->event_hs_wait_q);
  339. }
  340. static void btmrvl_free_adapter(struct btmrvl_private *priv)
  341. {
  342. skb_queue_purge(&priv->adapter->tx_queue);
  343. kfree(priv->adapter->hw_regs_buf);
  344. kfree(priv->adapter);
  345. priv->adapter = NULL;
  346. }
  347. static int btmrvl_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  348. {
  349. struct btmrvl_private *priv = hci_get_drvdata(hdev);
  350. BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len);
  351. switch (bt_cb(skb)->pkt_type) {
  352. case HCI_COMMAND_PKT:
  353. hdev->stat.cmd_tx++;
  354. break;
  355. case HCI_ACLDATA_PKT:
  356. hdev->stat.acl_tx++;
  357. break;
  358. case HCI_SCODATA_PKT:
  359. hdev->stat.sco_tx++;
  360. break;
  361. }
  362. skb_queue_tail(&priv->adapter->tx_queue, skb);
  363. wake_up_interruptible(&priv->main_thread.wait_q);
  364. return 0;
  365. }
  366. static int btmrvl_flush(struct hci_dev *hdev)
  367. {
  368. struct btmrvl_private *priv = hci_get_drvdata(hdev);
  369. skb_queue_purge(&priv->adapter->tx_queue);
  370. return 0;
  371. }
  372. static int btmrvl_close(struct hci_dev *hdev)
  373. {
  374. struct btmrvl_private *priv = hci_get_drvdata(hdev);
  375. skb_queue_purge(&priv->adapter->tx_queue);
  376. return 0;
  377. }
  378. static int btmrvl_open(struct hci_dev *hdev)
  379. {
  380. return 0;
  381. }
  382. static int btmrvl_download_cal_data(struct btmrvl_private *priv,
  383. u8 *data, int len)
  384. {
  385. int ret;
  386. data[0] = 0x00;
  387. data[1] = 0x00;
  388. data[2] = 0x00;
  389. data[3] = len;
  390. print_hex_dump_bytes("Calibration data: ",
  391. DUMP_PREFIX_OFFSET, data, BT_CAL_HDR_LEN + len);
  392. ret = btmrvl_send_sync_cmd(priv, BT_CMD_LOAD_CONFIG_DATA, data,
  393. BT_CAL_HDR_LEN + len);
  394. if (ret)
  395. BT_ERR("Failed to download caibration data");
  396. return 0;
  397. }
  398. static int btmrvl_check_device_tree(struct btmrvl_private *priv)
  399. {
  400. struct device_node *dt_node;
  401. u8 cal_data[BT_CAL_HDR_LEN + BT_CAL_DATA_SIZE];
  402. int ret;
  403. u32 val;
  404. for_each_compatible_node(dt_node, NULL, "btmrvl,cfgdata") {
  405. ret = of_property_read_u32(dt_node, "btmrvl,gpio-gap", &val);
  406. if (!ret)
  407. priv->btmrvl_dev.gpio_gap = val;
  408. ret = of_property_read_u8_array(dt_node, "btmrvl,cal-data",
  409. cal_data + BT_CAL_HDR_LEN,
  410. BT_CAL_DATA_SIZE);
  411. if (ret) {
  412. of_node_put(dt_node);
  413. return ret;
  414. }
  415. BT_DBG("Use cal data from device tree");
  416. ret = btmrvl_download_cal_data(priv, cal_data,
  417. BT_CAL_DATA_SIZE);
  418. if (ret) {
  419. BT_ERR("Fail to download calibrate data");
  420. of_node_put(dt_node);
  421. return ret;
  422. }
  423. }
  424. return 0;
  425. }
  426. static int btmrvl_setup(struct hci_dev *hdev)
  427. {
  428. struct btmrvl_private *priv = hci_get_drvdata(hdev);
  429. int ret;
  430. ret = btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
  431. if (ret)
  432. return ret;
  433. priv->btmrvl_dev.gpio_gap = 0xffff;
  434. btmrvl_check_device_tree(priv);
  435. btmrvl_enable_sco_routing_to_host(priv);
  436. btmrvl_pscan_window_reporting(priv, 0x01);
  437. priv->btmrvl_dev.psmode = 1;
  438. btmrvl_enable_ps(priv);
  439. btmrvl_send_hscfg_cmd(priv);
  440. return 0;
  441. }
  442. static int btmrvl_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  443. {
  444. struct sk_buff *skb;
  445. long ret;
  446. u8 buf[8];
  447. buf[0] = MRVL_VENDOR_PKT;
  448. buf[1] = sizeof(bdaddr_t);
  449. memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
  450. skb = __hci_cmd_sync(hdev, BT_CMD_SET_BDADDR, sizeof(buf), buf,
  451. HCI_INIT_TIMEOUT);
  452. if (IS_ERR(skb)) {
  453. ret = PTR_ERR(skb);
  454. BT_ERR("%s: changing btmrvl device address failed (%ld)",
  455. hdev->name, ret);
  456. return ret;
  457. }
  458. kfree_skb(skb);
  459. return 0;
  460. }
  461. /*
  462. * This function handles the event generated by firmware, rx data
  463. * received from firmware, and tx data sent from kernel.
  464. */
  465. static int btmrvl_service_main_thread(void *data)
  466. {
  467. struct btmrvl_thread *thread = data;
  468. struct btmrvl_private *priv = thread->priv;
  469. struct btmrvl_adapter *adapter = priv->adapter;
  470. wait_queue_t wait;
  471. struct sk_buff *skb;
  472. ulong flags;
  473. init_waitqueue_entry(&wait, current);
  474. for (;;) {
  475. add_wait_queue(&thread->wait_q, &wait);
  476. set_current_state(TASK_INTERRUPTIBLE);
  477. if (kthread_should_stop() || priv->surprise_removed) {
  478. BT_DBG("main_thread: break from main thread");
  479. break;
  480. }
  481. if (adapter->wakeup_tries ||
  482. ((!adapter->int_count) &&
  483. (!priv->btmrvl_dev.tx_dnld_rdy ||
  484. skb_queue_empty(&adapter->tx_queue)))) {
  485. BT_DBG("main_thread is sleeping...");
  486. schedule();
  487. }
  488. set_current_state(TASK_RUNNING);
  489. remove_wait_queue(&thread->wait_q, &wait);
  490. BT_DBG("main_thread woke up");
  491. if (kthread_should_stop() || priv->surprise_removed) {
  492. BT_DBG("main_thread: break from main thread");
  493. break;
  494. }
  495. spin_lock_irqsave(&priv->driver_lock, flags);
  496. if (adapter->int_count) {
  497. adapter->int_count = 0;
  498. spin_unlock_irqrestore(&priv->driver_lock, flags);
  499. priv->hw_process_int_status(priv);
  500. } else if (adapter->ps_state == PS_SLEEP &&
  501. !skb_queue_empty(&adapter->tx_queue)) {
  502. spin_unlock_irqrestore(&priv->driver_lock, flags);
  503. adapter->wakeup_tries++;
  504. priv->hw_wakeup_firmware(priv);
  505. continue;
  506. } else {
  507. spin_unlock_irqrestore(&priv->driver_lock, flags);
  508. }
  509. if (adapter->ps_state == PS_SLEEP)
  510. continue;
  511. if (!priv->btmrvl_dev.tx_dnld_rdy)
  512. continue;
  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
  518. priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
  519. kfree_skb(skb);
  520. }
  521. }
  522. return 0;
  523. }
  524. int btmrvl_register_hdev(struct btmrvl_private *priv)
  525. {
  526. struct hci_dev *hdev = NULL;
  527. int ret;
  528. hdev = hci_alloc_dev();
  529. if (!hdev) {
  530. BT_ERR("Can not allocate HCI device");
  531. goto err_hdev;
  532. }
  533. priv->btmrvl_dev.hcidev = hdev;
  534. hci_set_drvdata(hdev, priv);
  535. hdev->bus = HCI_SDIO;
  536. hdev->open = btmrvl_open;
  537. hdev->close = btmrvl_close;
  538. hdev->flush = btmrvl_flush;
  539. hdev->send = btmrvl_send_frame;
  540. hdev->setup = btmrvl_setup;
  541. hdev->set_bdaddr = btmrvl_set_bdaddr;
  542. hdev->dev_type = priv->btmrvl_dev.dev_type;
  543. ret = hci_register_dev(hdev);
  544. if (ret < 0) {
  545. BT_ERR("Can not register HCI device");
  546. goto err_hci_register_dev;
  547. }
  548. #ifdef CONFIG_DEBUG_FS
  549. btmrvl_debugfs_init(hdev);
  550. #endif
  551. return 0;
  552. err_hci_register_dev:
  553. hci_free_dev(hdev);
  554. err_hdev:
  555. /* Stop the thread servicing the interrupts */
  556. kthread_stop(priv->main_thread.task);
  557. btmrvl_free_adapter(priv);
  558. kfree(priv);
  559. return -ENOMEM;
  560. }
  561. EXPORT_SYMBOL_GPL(btmrvl_register_hdev);
  562. struct btmrvl_private *btmrvl_add_card(void *card)
  563. {
  564. struct btmrvl_private *priv;
  565. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  566. if (!priv) {
  567. BT_ERR("Can not allocate priv");
  568. goto err_priv;
  569. }
  570. priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
  571. if (!priv->adapter) {
  572. BT_ERR("Allocate buffer for btmrvl_adapter failed!");
  573. goto err_adapter;
  574. }
  575. btmrvl_init_adapter(priv);
  576. BT_DBG("Starting kthread...");
  577. priv->main_thread.priv = priv;
  578. spin_lock_init(&priv->driver_lock);
  579. init_waitqueue_head(&priv->main_thread.wait_q);
  580. priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
  581. &priv->main_thread, "btmrvl_main_service");
  582. if (IS_ERR(priv->main_thread.task))
  583. goto err_thread;
  584. priv->btmrvl_dev.card = card;
  585. priv->btmrvl_dev.tx_dnld_rdy = true;
  586. return priv;
  587. err_thread:
  588. btmrvl_free_adapter(priv);
  589. err_adapter:
  590. kfree(priv);
  591. err_priv:
  592. return NULL;
  593. }
  594. EXPORT_SYMBOL_GPL(btmrvl_add_card);
  595. int btmrvl_remove_card(struct btmrvl_private *priv)
  596. {
  597. struct hci_dev *hdev;
  598. hdev = priv->btmrvl_dev.hcidev;
  599. wake_up_interruptible(&priv->adapter->cmd_wait_q);
  600. wake_up_interruptible(&priv->adapter->event_hs_wait_q);
  601. kthread_stop(priv->main_thread.task);
  602. #ifdef CONFIG_DEBUG_FS
  603. btmrvl_debugfs_remove(hdev);
  604. #endif
  605. hci_unregister_dev(hdev);
  606. hci_free_dev(hdev);
  607. priv->btmrvl_dev.hcidev = NULL;
  608. btmrvl_free_adapter(priv);
  609. kfree(priv);
  610. return 0;
  611. }
  612. EXPORT_SYMBOL_GPL(btmrvl_remove_card);
  613. MODULE_AUTHOR("Marvell International Ltd.");
  614. MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
  615. MODULE_VERSION(VERSION);
  616. MODULE_LICENSE("GPL v2");