btmrvl_main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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_PRIMARY;
  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_KERNEL);
  158. if (!skb) {
  159. BT_ERR("No free skb");
  160. return -ENOMEM;
  161. }
  162. hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
  163. hdr->opcode = cpu_to_le16(opcode);
  164. hdr->plen = len;
  165. if (len)
  166. skb_put_data(skb, param, len);
  167. hci_skb_pkt_type(skb) = 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. static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
  293. {
  294. int ret = 0;
  295. if (!skb || !skb->data)
  296. return -EINVAL;
  297. if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
  298. BT_ERR("Tx Error: Bad skb length %d : %d",
  299. skb->len, BTM_UPLD_SIZE);
  300. return -EINVAL;
  301. }
  302. skb_push(skb, BTM_HEADER_LEN);
  303. /* header type: byte[3]
  304. * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
  305. * header length: byte[2][1][0]
  306. */
  307. skb->data[0] = (skb->len & 0x0000ff);
  308. skb->data[1] = (skb->len & 0x00ff00) >> 8;
  309. skb->data[2] = (skb->len & 0xff0000) >> 16;
  310. skb->data[3] = hci_skb_pkt_type(skb);
  311. if (priv->hw_host_to_card)
  312. ret = priv->hw_host_to_card(priv, skb->data, skb->len);
  313. return ret;
  314. }
  315. static void btmrvl_init_adapter(struct btmrvl_private *priv)
  316. {
  317. int buf_size;
  318. skb_queue_head_init(&priv->adapter->tx_queue);
  319. priv->adapter->ps_state = PS_AWAKE;
  320. buf_size = ALIGN_SZ(SDIO_BLOCK_SIZE, BTSDIO_DMA_ALIGN);
  321. priv->adapter->hw_regs_buf = kzalloc(buf_size, GFP_KERNEL);
  322. if (!priv->adapter->hw_regs_buf) {
  323. priv->adapter->hw_regs = NULL;
  324. BT_ERR("Unable to allocate buffer for hw_regs.");
  325. } else {
  326. priv->adapter->hw_regs =
  327. (u8 *)ALIGN_ADDR(priv->adapter->hw_regs_buf,
  328. BTSDIO_DMA_ALIGN);
  329. BT_DBG("hw_regs_buf=%p hw_regs=%p",
  330. priv->adapter->hw_regs_buf, priv->adapter->hw_regs);
  331. }
  332. init_waitqueue_head(&priv->adapter->cmd_wait_q);
  333. init_waitqueue_head(&priv->adapter->event_hs_wait_q);
  334. }
  335. static void btmrvl_free_adapter(struct btmrvl_private *priv)
  336. {
  337. skb_queue_purge(&priv->adapter->tx_queue);
  338. kfree(priv->adapter->hw_regs_buf);
  339. kfree(priv->adapter);
  340. priv->adapter = NULL;
  341. }
  342. static int btmrvl_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  343. {
  344. struct btmrvl_private *priv = hci_get_drvdata(hdev);
  345. BT_DBG("type=%d, len=%d", hci_skb_pkt_type(skb), skb->len);
  346. if (priv->adapter->is_suspending || priv->adapter->is_suspended) {
  347. BT_ERR("%s: Device is suspending or suspended", __func__);
  348. return -EBUSY;
  349. }
  350. switch (hci_skb_pkt_type(skb)) {
  351. case HCI_COMMAND_PKT:
  352. hdev->stat.cmd_tx++;
  353. break;
  354. case HCI_ACLDATA_PKT:
  355. hdev->stat.acl_tx++;
  356. break;
  357. case HCI_SCODATA_PKT:
  358. hdev->stat.sco_tx++;
  359. break;
  360. }
  361. skb_queue_tail(&priv->adapter->tx_queue, skb);
  362. if (!priv->adapter->is_suspended)
  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 calibration data");
  396. return 0;
  397. }
  398. static int btmrvl_check_device_tree(struct btmrvl_private *priv)
  399. {
  400. struct device_node *dt_node;
  401. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  402. u8 cal_data[BT_CAL_HDR_LEN + BT_CAL_DATA_SIZE];
  403. int ret = 0;
  404. u16 gpio, gap;
  405. if (card->plt_of_node) {
  406. dt_node = card->plt_of_node;
  407. ret = of_property_read_u16(dt_node, "marvell,wakeup-pin",
  408. &gpio);
  409. if (ret)
  410. gpio = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
  411. ret = of_property_read_u16(dt_node, "marvell,wakeup-gap-ms",
  412. &gap);
  413. if (ret)
  414. gap = (u8)(priv->btmrvl_dev.gpio_gap & 0x00ff);
  415. priv->btmrvl_dev.gpio_gap = (gpio << 8) + gap;
  416. ret = of_property_read_u8_array(dt_node, "marvell,cal-data",
  417. cal_data + BT_CAL_HDR_LEN,
  418. BT_CAL_DATA_SIZE);
  419. if (ret)
  420. return ret;
  421. BT_DBG("Use cal data from device tree");
  422. ret = btmrvl_download_cal_data(priv, cal_data,
  423. BT_CAL_DATA_SIZE);
  424. if (ret)
  425. BT_ERR("Fail to download calibrate data");
  426. }
  427. return ret;
  428. }
  429. static int btmrvl_setup(struct hci_dev *hdev)
  430. {
  431. struct btmrvl_private *priv = hci_get_drvdata(hdev);
  432. int ret;
  433. ret = btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
  434. if (ret)
  435. return ret;
  436. priv->btmrvl_dev.gpio_gap = 0xfffe;
  437. btmrvl_check_device_tree(priv);
  438. btmrvl_enable_sco_routing_to_host(priv);
  439. btmrvl_pscan_window_reporting(priv, 0x01);
  440. priv->btmrvl_dev.psmode = 1;
  441. btmrvl_enable_ps(priv);
  442. btmrvl_send_hscfg_cmd(priv);
  443. return 0;
  444. }
  445. static int btmrvl_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  446. {
  447. struct sk_buff *skb;
  448. long ret;
  449. u8 buf[8];
  450. buf[0] = MRVL_VENDOR_PKT;
  451. buf[1] = sizeof(bdaddr_t);
  452. memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
  453. skb = __hci_cmd_sync(hdev, BT_CMD_SET_BDADDR, sizeof(buf), buf,
  454. HCI_INIT_TIMEOUT);
  455. if (IS_ERR(skb)) {
  456. ret = PTR_ERR(skb);
  457. BT_ERR("%s: changing btmrvl device address failed (%ld)",
  458. hdev->name, ret);
  459. return ret;
  460. }
  461. kfree_skb(skb);
  462. return 0;
  463. }
  464. /*
  465. * This function handles the event generated by firmware, rx data
  466. * received from firmware, and tx data sent from kernel.
  467. */
  468. static int btmrvl_service_main_thread(void *data)
  469. {
  470. struct btmrvl_thread *thread = data;
  471. struct btmrvl_private *priv = thread->priv;
  472. struct btmrvl_adapter *adapter = priv->adapter;
  473. wait_queue_entry_t wait;
  474. struct sk_buff *skb;
  475. ulong flags;
  476. init_waitqueue_entry(&wait, current);
  477. for (;;) {
  478. add_wait_queue(&thread->wait_q, &wait);
  479. set_current_state(TASK_INTERRUPTIBLE);
  480. if (kthread_should_stop() || priv->surprise_removed) {
  481. BT_DBG("main_thread: break from main thread");
  482. break;
  483. }
  484. if (adapter->wakeup_tries ||
  485. ((!adapter->int_count) &&
  486. (!priv->btmrvl_dev.tx_dnld_rdy ||
  487. skb_queue_empty(&adapter->tx_queue)))) {
  488. BT_DBG("main_thread is sleeping...");
  489. schedule();
  490. }
  491. set_current_state(TASK_RUNNING);
  492. remove_wait_queue(&thread->wait_q, &wait);
  493. BT_DBG("main_thread woke up");
  494. if (kthread_should_stop() || priv->surprise_removed) {
  495. BT_DBG("main_thread: break from main thread");
  496. break;
  497. }
  498. spin_lock_irqsave(&priv->driver_lock, flags);
  499. if (adapter->int_count) {
  500. adapter->int_count = 0;
  501. spin_unlock_irqrestore(&priv->driver_lock, flags);
  502. priv->hw_process_int_status(priv);
  503. } else if (adapter->ps_state == PS_SLEEP &&
  504. !skb_queue_empty(&adapter->tx_queue)) {
  505. spin_unlock_irqrestore(&priv->driver_lock, flags);
  506. adapter->wakeup_tries++;
  507. priv->hw_wakeup_firmware(priv);
  508. continue;
  509. } else {
  510. spin_unlock_irqrestore(&priv->driver_lock, flags);
  511. }
  512. if (adapter->ps_state == PS_SLEEP)
  513. continue;
  514. if (!priv->btmrvl_dev.tx_dnld_rdy ||
  515. priv->adapter->is_suspended)
  516. continue;
  517. skb = skb_dequeue(&adapter->tx_queue);
  518. if (skb) {
  519. if (btmrvl_tx_pkt(priv, skb))
  520. priv->btmrvl_dev.hcidev->stat.err_tx++;
  521. else
  522. priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
  523. kfree_skb(skb);
  524. }
  525. }
  526. return 0;
  527. }
  528. int btmrvl_register_hdev(struct btmrvl_private *priv)
  529. {
  530. struct hci_dev *hdev = NULL;
  531. int ret;
  532. hdev = hci_alloc_dev();
  533. if (!hdev) {
  534. BT_ERR("Can not allocate HCI device");
  535. goto err_hdev;
  536. }
  537. priv->btmrvl_dev.hcidev = hdev;
  538. hci_set_drvdata(hdev, priv);
  539. hdev->bus = HCI_SDIO;
  540. hdev->open = btmrvl_open;
  541. hdev->close = btmrvl_close;
  542. hdev->flush = btmrvl_flush;
  543. hdev->send = btmrvl_send_frame;
  544. hdev->setup = btmrvl_setup;
  545. hdev->set_bdaddr = btmrvl_set_bdaddr;
  546. hdev->dev_type = priv->btmrvl_dev.dev_type;
  547. ret = hci_register_dev(hdev);
  548. if (ret < 0) {
  549. BT_ERR("Can not register HCI device");
  550. goto err_hci_register_dev;
  551. }
  552. #ifdef CONFIG_DEBUG_FS
  553. btmrvl_debugfs_init(hdev);
  554. #endif
  555. return 0;
  556. err_hci_register_dev:
  557. hci_free_dev(hdev);
  558. err_hdev:
  559. /* Stop the thread servicing the interrupts */
  560. kthread_stop(priv->main_thread.task);
  561. btmrvl_free_adapter(priv);
  562. kfree(priv);
  563. return -ENOMEM;
  564. }
  565. EXPORT_SYMBOL_GPL(btmrvl_register_hdev);
  566. struct btmrvl_private *btmrvl_add_card(void *card)
  567. {
  568. struct btmrvl_private *priv;
  569. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  570. if (!priv) {
  571. BT_ERR("Can not allocate priv");
  572. goto err_priv;
  573. }
  574. priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
  575. if (!priv->adapter) {
  576. BT_ERR("Allocate buffer for btmrvl_adapter failed!");
  577. goto err_adapter;
  578. }
  579. btmrvl_init_adapter(priv);
  580. BT_DBG("Starting kthread...");
  581. priv->main_thread.priv = priv;
  582. spin_lock_init(&priv->driver_lock);
  583. init_waitqueue_head(&priv->main_thread.wait_q);
  584. priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
  585. &priv->main_thread, "btmrvl_main_service");
  586. if (IS_ERR(priv->main_thread.task))
  587. goto err_thread;
  588. priv->btmrvl_dev.card = card;
  589. priv->btmrvl_dev.tx_dnld_rdy = true;
  590. return priv;
  591. err_thread:
  592. btmrvl_free_adapter(priv);
  593. err_adapter:
  594. kfree(priv);
  595. err_priv:
  596. return NULL;
  597. }
  598. EXPORT_SYMBOL_GPL(btmrvl_add_card);
  599. int btmrvl_remove_card(struct btmrvl_private *priv)
  600. {
  601. struct hci_dev *hdev;
  602. hdev = priv->btmrvl_dev.hcidev;
  603. wake_up_interruptible(&priv->adapter->cmd_wait_q);
  604. wake_up_interruptible(&priv->adapter->event_hs_wait_q);
  605. kthread_stop(priv->main_thread.task);
  606. #ifdef CONFIG_DEBUG_FS
  607. btmrvl_debugfs_remove(hdev);
  608. #endif
  609. hci_unregister_dev(hdev);
  610. hci_free_dev(hdev);
  611. priv->btmrvl_dev.hcidev = NULL;
  612. btmrvl_free_adapter(priv);
  613. kfree(priv);
  614. return 0;
  615. }
  616. EXPORT_SYMBOL_GPL(btmrvl_remove_card);
  617. MODULE_AUTHOR("Marvell International Ltd.");
  618. MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
  619. MODULE_VERSION(VERSION);
  620. MODULE_LICENSE("GPL v2");