PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/bluetooth/hci_smd.c

https://bitbucket.org/thenameisnigel/android_kernel_lge_ls840
C | 560 lines | 425 code | 78 blank | 57 comment | 49 complexity | 52c31e49e9874020f0904bb1dabd8ed4 MD5 | raw file
  1. /*
  2. * HCI_SMD (HCI Shared Memory Driver) is Qualcomm's Shared memory driver
  3. * for the BT HCI protocol.
  4. *
  5. * Copyright (c) 2000-2001, 2011-2012 Code Aurora Forum. All rights reserved.
  6. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  7. * Copyright (C) 2004-2006 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. * This file is based on drivers/bluetooth/hci_vhci.c
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2
  13. * as published by the Free Software Foundation
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/errno.h>
  24. #include <linux/string.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/wakelock.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/uaccess.h>
  29. #include <net/bluetooth/bluetooth.h>
  30. #include <net/bluetooth/hci_core.h>
  31. #include <net/bluetooth/hci.h>
  32. #include <mach/msm_smd.h>
  33. #define EVENT_CHANNEL "APPS_RIVA_BT_CMD"
  34. #define DATA_CHANNEL "APPS_RIVA_BT_ACL"
  35. /* release wakelock in 500ms, not immediately, because higher layers
  36. * don't always take wakelocks when they should
  37. * This is derived from the implementation for UART transport
  38. */
  39. #define RX_Q_MONITOR (500) /* 500 milli second */
  40. static int hcismd_set;
  41. static DEFINE_MUTEX(hci_smd_enable);
  42. static int hcismd_set_enable(const char *val, struct kernel_param *kp);
  43. module_param_call(hcismd_set, hcismd_set_enable, NULL, &hcismd_set, 0644);
  44. static void hci_dev_smd_open(struct work_struct *worker);
  45. static void hci_dev_restart(struct work_struct *worker);
  46. struct hci_smd_data {
  47. struct hci_dev *hdev;
  48. struct smd_channel *event_channel;
  49. struct smd_channel *data_channel;
  50. struct wake_lock wake_lock_tx;
  51. struct wake_lock wake_lock_rx;
  52. struct timer_list rx_q_timer;
  53. struct tasklet_struct rx_task;
  54. };
  55. static struct hci_smd_data hs;
  56. /* Rx queue monitor timer function */
  57. static int is_rx_q_empty(unsigned long arg)
  58. {
  59. struct hci_dev *hdev = (struct hci_dev *) arg;
  60. struct sk_buff_head *list_ = &hdev->rx_q;
  61. struct sk_buff *list = ((struct sk_buff *)list_)->next;
  62. BT_DBG("%s Rx timer triggered", hdev->name);
  63. if (list == (struct sk_buff *)list_) {
  64. BT_DBG("%s RX queue empty", hdev->name);
  65. return 1;
  66. } else{
  67. BT_DBG("%s RX queue not empty", hdev->name);
  68. return 0;
  69. }
  70. }
  71. static void release_lock(void)
  72. {
  73. struct hci_smd_data *hsmd = &hs;
  74. BT_DBG("Releasing Rx Lock");
  75. if (is_rx_q_empty((unsigned long)hsmd->hdev) &&
  76. wake_lock_active(&hs.wake_lock_rx))
  77. wake_unlock(&hs.wake_lock_rx);
  78. }
  79. /* Rx timer callback function */
  80. static void schedule_timer(unsigned long arg)
  81. {
  82. struct hci_dev *hdev = (struct hci_dev *) arg;
  83. struct hci_smd_data *hsmd = &hs;
  84. BT_DBG("%s Schedule Rx timer", hdev->name);
  85. if (is_rx_q_empty(arg) && wake_lock_active(&hs.wake_lock_rx)) {
  86. BT_DBG("%s RX queue empty", hdev->name);
  87. /*
  88. * Since the queue is empty, its ideal
  89. * to release the wake lock on Rx
  90. */
  91. wake_unlock(&hs.wake_lock_rx);
  92. } else{
  93. BT_DBG("%s RX queue not empty", hdev->name);
  94. /*
  95. * Restart the timer to monitor whether the Rx queue is
  96. * empty for releasing the Rx wake lock
  97. */
  98. mod_timer(&hsmd->rx_q_timer,
  99. jiffies + msecs_to_jiffies(RX_Q_MONITOR));
  100. }
  101. }
  102. static int hci_smd_open(struct hci_dev *hdev)
  103. {
  104. set_bit(HCI_RUNNING, &hdev->flags);
  105. return 0;
  106. }
  107. static int hci_smd_close(struct hci_dev *hdev)
  108. {
  109. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  110. return 0;
  111. else
  112. return -EPERM;
  113. }
  114. static void hci_smd_destruct(struct hci_dev *hdev)
  115. {
  116. if (NULL != hdev->driver_data)
  117. kfree(hdev->driver_data);
  118. }
  119. static void hci_smd_recv_data(void)
  120. {
  121. int len = 0;
  122. int rc = 0;
  123. struct sk_buff *skb = NULL;
  124. struct hci_smd_data *hsmd = &hs;
  125. wake_lock(&hs.wake_lock_rx);
  126. len = smd_read_avail(hsmd->data_channel);
  127. if (len > HCI_MAX_FRAME_SIZE) {
  128. BT_ERR("Frame larger than the allowed size, flushing frame");
  129. smd_read(hsmd->data_channel, NULL, len);
  130. goto out_data;
  131. }
  132. if (len <= 0)
  133. goto out_data;
  134. skb = bt_skb_alloc(len, GFP_ATOMIC);
  135. if (!skb) {
  136. BT_ERR("Error in allocating socket buffer");
  137. smd_read(hsmd->data_channel, NULL, len);
  138. goto out_data;
  139. }
  140. rc = smd_read(hsmd->data_channel, skb_put(skb, len), len);
  141. if (rc < len) {
  142. BT_ERR("Error in reading from the channel");
  143. goto out_data;
  144. }
  145. skb->dev = (void *)hsmd->hdev;
  146. bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
  147. skb_orphan(skb);
  148. rc = hci_recv_frame(skb);
  149. if (rc < 0) {
  150. BT_ERR("Error in passing the packet to HCI Layer");
  151. /*
  152. * skb is getting freed in hci_recv_frame, making it
  153. * to null to avoid multiple access
  154. */
  155. skb = NULL;
  156. goto out_data;
  157. }
  158. /*
  159. * Start the timer to monitor whether the Rx queue is
  160. * empty for releasing the Rx wake lock
  161. */
  162. BT_DBG("Rx Timer is starting");
  163. mod_timer(&hsmd->rx_q_timer,
  164. jiffies + msecs_to_jiffies(RX_Q_MONITOR));
  165. out_data:
  166. release_lock();
  167. if (rc)
  168. kfree_skb(skb);
  169. }
  170. static void hci_smd_recv_event(void)
  171. {
  172. int len = 0;
  173. int rc = 0;
  174. struct sk_buff *skb = NULL;
  175. struct hci_smd_data *hsmd = &hs;
  176. wake_lock(&hs.wake_lock_rx);
  177. len = smd_read_avail(hsmd->event_channel);
  178. if (len > HCI_MAX_FRAME_SIZE) {
  179. BT_ERR("Frame larger than the allowed size, flushing frame");
  180. rc = smd_read(hsmd->event_channel, NULL, len);
  181. goto out_event;
  182. }
  183. while (len > 0) {
  184. skb = bt_skb_alloc(len, GFP_ATOMIC);
  185. if (!skb) {
  186. BT_ERR("Error in allocating socket buffer");
  187. smd_read(hsmd->event_channel, NULL, len);
  188. goto out_event;
  189. }
  190. rc = smd_read(hsmd->event_channel, skb_put(skb, len), len);
  191. if (rc < len) {
  192. BT_ERR("Error in reading from the event channel");
  193. goto out_event;
  194. }
  195. skb->dev = (void *)hsmd->hdev;
  196. bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
  197. skb_orphan(skb);
  198. rc = hci_recv_frame(skb);
  199. if (rc < 0) {
  200. BT_ERR("Error in passing the packet to HCI Layer");
  201. /*
  202. * skb is getting freed in hci_recv_frame, making it
  203. * to null to avoid multiple access
  204. */
  205. skb = NULL;
  206. goto out_event;
  207. }
  208. len = smd_read_avail(hsmd->event_channel);
  209. /*
  210. * Start the timer to monitor whether the Rx queue is
  211. * empty for releasing the Rx wake lock
  212. */
  213. BT_DBG("Rx Timer is starting");
  214. mod_timer(&hsmd->rx_q_timer,
  215. jiffies + msecs_to_jiffies(RX_Q_MONITOR));
  216. }
  217. out_event:
  218. release_lock();
  219. if (rc)
  220. kfree_skb(skb);
  221. }
  222. static int hci_smd_send_frame(struct sk_buff *skb)
  223. {
  224. int len;
  225. int avail;
  226. int ret = 0;
  227. wake_lock(&hs.wake_lock_tx);
  228. switch (bt_cb(skb)->pkt_type) {
  229. case HCI_COMMAND_PKT:
  230. avail = smd_write_avail(hs.event_channel);
  231. if (!avail) {
  232. BT_ERR("No space available for smd frame");
  233. ret = -ENOSPC;
  234. }
  235. len = smd_write(hs.event_channel, skb->data, skb->len);
  236. if (len < skb->len) {
  237. BT_ERR("Failed to write Command %d", len);
  238. ret = -ENODEV;
  239. }
  240. break;
  241. case HCI_ACLDATA_PKT:
  242. case HCI_SCODATA_PKT:
  243. avail = smd_write_avail(hs.data_channel);
  244. if (!avail) {
  245. BT_ERR("No space available for smd frame");
  246. ret = -ENOSPC;
  247. }
  248. len = smd_write(hs.data_channel, skb->data, skb->len);
  249. if (len < skb->len) {
  250. BT_ERR("Failed to write Data %d", len);
  251. ret = -ENODEV;
  252. }
  253. break;
  254. default:
  255. BT_ERR("Uknown packet type");
  256. ret = -ENODEV;
  257. break;
  258. }
  259. kfree_skb(skb);
  260. wake_unlock(&hs.wake_lock_tx);
  261. return ret;
  262. }
  263. static void hci_smd_rx(unsigned long arg)
  264. {
  265. struct hci_smd_data *hsmd = &hs;
  266. while ((smd_read_avail(hsmd->event_channel) > 0) ||
  267. (smd_read_avail(hsmd->data_channel) > 0)) {
  268. hci_smd_recv_event();
  269. hci_smd_recv_data();
  270. }
  271. }
  272. static void hci_smd_notify_event(void *data, unsigned int event)
  273. {
  274. struct hci_dev *hdev = hs.hdev;
  275. struct hci_smd_data *hsmd = &hs;
  276. struct work_struct *reset_worker;
  277. struct work_struct *open_worker;
  278. int len = 0;
  279. if (!hdev) {
  280. BT_ERR("Frame for unknown HCI device (hdev=NULL)");
  281. return;
  282. }
  283. switch (event) {
  284. case SMD_EVENT_DATA:
  285. len = smd_read_avail(hsmd->event_channel);
  286. if (len > 0)
  287. tasklet_hi_schedule(&hs.rx_task);
  288. else if (len < 0)
  289. BT_ERR("Failed to read event from smd %d", len);
  290. break;
  291. case SMD_EVENT_OPEN:
  292. BT_INFO("opening HCI-SMD channel :%s", EVENT_CHANNEL);
  293. hci_smd_open(hdev);
  294. open_worker = kzalloc(sizeof(*open_worker), GFP_ATOMIC);
  295. if (!open_worker) {
  296. BT_ERR("Out of memory");
  297. break;
  298. }
  299. INIT_WORK(open_worker, hci_dev_smd_open);
  300. schedule_work(open_worker);
  301. break;
  302. case SMD_EVENT_CLOSE:
  303. BT_INFO("Closing HCI-SMD channel :%s", EVENT_CHANNEL);
  304. hci_smd_close(hdev);
  305. reset_worker = kzalloc(sizeof(*reset_worker), GFP_ATOMIC);
  306. if (!reset_worker) {
  307. BT_ERR("Out of memory");
  308. break;
  309. }
  310. INIT_WORK(reset_worker, hci_dev_restart);
  311. schedule_work(reset_worker);
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. static void hci_smd_notify_data(void *data, unsigned int event)
  318. {
  319. struct hci_dev *hdev = hs.hdev;
  320. struct hci_smd_data *hsmd = &hs;
  321. int len = 0;
  322. if (!hdev) {
  323. BT_ERR("Frame for unknown HCI device (hdev=NULL)");
  324. return;
  325. }
  326. switch (event) {
  327. case SMD_EVENT_DATA:
  328. len = smd_read_avail(hsmd->data_channel);
  329. if (len > 0)
  330. tasklet_hi_schedule(&hs.rx_task);
  331. else if (len < 0)
  332. BT_ERR("Failed to read data from smd %d", len);
  333. break;
  334. case SMD_EVENT_OPEN:
  335. BT_INFO("opening HCI-SMD channel :%s", DATA_CHANNEL);
  336. hci_smd_open(hdev);
  337. break;
  338. case SMD_EVENT_CLOSE:
  339. BT_INFO("Closing HCI-SMD channel :%s", DATA_CHANNEL);
  340. hci_smd_close(hdev);
  341. break;
  342. default:
  343. break;
  344. }
  345. }
  346. static int hci_smd_hci_register_dev(struct hci_smd_data *hsmd)
  347. {
  348. struct hci_dev *hdev;
  349. hdev = hsmd->hdev;
  350. if (hci_register_dev(hdev) < 0) {
  351. BT_ERR("Can't register HCI device");
  352. hci_free_dev(hdev);
  353. hsmd->hdev = NULL;
  354. return -ENODEV;
  355. }
  356. return 0;
  357. }
  358. static int hci_smd_register_smd(struct hci_smd_data *hsmd)
  359. {
  360. struct hci_dev *hdev;
  361. int rc;
  362. /* Initialize and register HCI device */
  363. hdev = hci_alloc_dev();
  364. if (!hdev) {
  365. BT_ERR("Can't allocate HCI device");
  366. return -ENOMEM;
  367. }
  368. hsmd->hdev = hdev;
  369. hdev->bus = HCI_SMD;
  370. hdev->driver_data = NULL;
  371. hdev->open = hci_smd_open;
  372. hdev->close = hci_smd_close;
  373. hdev->send = hci_smd_send_frame;
  374. hdev->destruct = hci_smd_destruct;
  375. hdev->owner = THIS_MODULE;
  376. tasklet_init(&hsmd->rx_task,
  377. hci_smd_rx, (unsigned long) hsmd);
  378. /*
  379. * Setup the timer to monitor whether the Rx queue is empty,
  380. * to control the wake lock release
  381. */
  382. setup_timer(&hsmd->rx_q_timer, schedule_timer,
  383. (unsigned long) hsmd->hdev);
  384. /* Open the SMD Channel and device and register the callback function */
  385. rc = smd_named_open_on_edge(EVENT_CHANNEL, SMD_APPS_WCNSS,
  386. &hsmd->event_channel, hdev, hci_smd_notify_event);
  387. if (rc < 0) {
  388. BT_ERR("Cannot open the command channel");
  389. hci_free_dev(hdev);
  390. hsmd->hdev = NULL;
  391. return -ENODEV;
  392. }
  393. rc = smd_named_open_on_edge(DATA_CHANNEL, SMD_APPS_WCNSS,
  394. &hsmd->data_channel, hdev, hci_smd_notify_data);
  395. if (rc < 0) {
  396. BT_ERR("Failed to open the Data channel");
  397. hci_free_dev(hdev);
  398. hsmd->hdev = NULL;
  399. return -ENODEV;
  400. }
  401. /* Disable the read interrupts on the channel */
  402. smd_disable_read_intr(hsmd->event_channel);
  403. smd_disable_read_intr(hsmd->data_channel);
  404. return 0;
  405. }
  406. static void hci_smd_deregister_dev(struct hci_smd_data *hsmd)
  407. {
  408. tasklet_kill(&hs.rx_task);
  409. if (hsmd->hdev) {
  410. if (hci_unregister_dev(hsmd->hdev) < 0)
  411. BT_ERR("Can't unregister HCI device %s",
  412. hsmd->hdev->name);
  413. hci_free_dev(hsmd->hdev);
  414. hsmd->hdev = NULL;
  415. }
  416. smd_close(hs.event_channel);
  417. smd_close(hs.data_channel);
  418. if (wake_lock_active(&hs.wake_lock_rx))
  419. wake_unlock(&hs.wake_lock_rx);
  420. if (wake_lock_active(&hs.wake_lock_tx))
  421. wake_unlock(&hs.wake_lock_tx);
  422. /*Destroy the timer used to monitor the Rx queue for emptiness */
  423. if (hs.rx_q_timer.function) {
  424. del_timer_sync(&hs.rx_q_timer);
  425. hs.rx_q_timer.function = NULL;
  426. hs.rx_q_timer.data = 0;
  427. }
  428. }
  429. static void hci_dev_restart(struct work_struct *worker)
  430. {
  431. mutex_lock(&hci_smd_enable);
  432. hci_smd_deregister_dev(&hs);
  433. hci_smd_register_smd(&hs);
  434. mutex_unlock(&hci_smd_enable);
  435. kfree(worker);
  436. }
  437. static void hci_dev_smd_open(struct work_struct *worker)
  438. {
  439. mutex_lock(&hci_smd_enable);
  440. hci_smd_hci_register_dev(&hs);
  441. mutex_unlock(&hci_smd_enable);
  442. kfree(worker);
  443. }
  444. static int hcismd_set_enable(const char *val, struct kernel_param *kp)
  445. {
  446. int ret = 0;
  447. mutex_lock(&hci_smd_enable);
  448. ret = param_set_int(val, kp);
  449. if (ret)
  450. goto done;
  451. switch (hcismd_set) {
  452. case 1:
  453. hci_smd_register_smd(&hs);
  454. break;
  455. case 0:
  456. hci_smd_deregister_dev(&hs);
  457. break;
  458. default:
  459. ret = -EFAULT;
  460. }
  461. done:
  462. mutex_unlock(&hci_smd_enable);
  463. return ret;
  464. }
  465. static int __init hci_smd_init(void)
  466. {
  467. wake_lock_init(&hs.wake_lock_rx, WAKE_LOCK_SUSPEND,
  468. "msm_smd_Rx");
  469. wake_lock_init(&hs.wake_lock_tx, WAKE_LOCK_SUSPEND,
  470. "msm_smd_Tx");
  471. return 0;
  472. }
  473. module_init(hci_smd_init);
  474. static void __exit hci_smd_exit(void)
  475. {
  476. wake_lock_destroy(&hs.wake_lock_rx);
  477. wake_lock_destroy(&hs.wake_lock_tx);
  478. }
  479. module_exit(hci_smd_exit);
  480. MODULE_AUTHOR("Ankur Nandwani <ankurn@codeaurora.org>");
  481. MODULE_DESCRIPTION("Bluetooth SMD driver");
  482. MODULE_LICENSE("GPL v2");