radio-iris-transport.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Qualcomm's FM Shared Memory Transport Driver
  3. *
  4. * FM HCI_SMD ( FM HCI Shared Memory Driver) is Qualcomm's Shared memory driver
  5. * for the HCI protocol. This file is based on drivers/bluetooth/hci_vhci.c
  6. *
  7. * Copyright (c) 2000-2001, 2011-2012, 2014-2015 The Linux Foundation.
  8. * All rights reserved.
  9. *
  10. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  11. * Copyright (C) 2004-2006 Marcel Holtmann <marcel@holtmann.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/string.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/workqueue.h>
  29. #include <mach/msm_smd.h>
  30. #include <media/radio-iris.h>
  31. #include <linux/wakelock.h>
  32. #include <linux/uaccess.h>
  33. struct radio_data {
  34. struct radio_hci_dev *hdev;
  35. struct tasklet_struct rx_task;
  36. struct smd_channel *fm_channel;
  37. };
  38. struct radio_data hs;
  39. DEFINE_MUTEX(fm_smd_enable);
  40. static int fmsmd_set;
  41. static bool chan_opened;
  42. static int hcismd_fm_set_enable(const char *val, struct kernel_param *kp);
  43. module_param_call(fmsmd_set, hcismd_fm_set_enable, NULL, &fmsmd_set, 0644);
  44. static struct work_struct *reset_worker;
  45. static void radio_hci_smd_deregister(void);
  46. static void radio_hci_smd_exit(void);
  47. static void radio_hci_smd_destruct(struct radio_hci_dev *hdev)
  48. {
  49. radio_hci_unregister_dev();
  50. }
  51. static void radio_hci_smd_recv_event(unsigned long temp)
  52. {
  53. int len;
  54. int rc;
  55. struct sk_buff *skb;
  56. unsigned char *buf;
  57. struct radio_data *hsmd = &hs;
  58. len = smd_read_avail(hsmd->fm_channel);
  59. while (len) {
  60. skb = alloc_skb(len, GFP_ATOMIC);
  61. if (!skb) {
  62. FMDERR("Memory not allocated for the socket");
  63. return;
  64. }
  65. buf = kmalloc(len, GFP_ATOMIC);
  66. if (!buf) {
  67. kfree_skb(skb);
  68. FMDERR("Error in allocating buffer memory");
  69. return;
  70. }
  71. rc = smd_read(hsmd->fm_channel, (void *)buf, len);
  72. memcpy(skb_put(skb, len), buf, len);
  73. skb_orphan(skb);
  74. skb->dev = (struct net_device *)hs.hdev;
  75. rc = radio_hci_recv_frame(skb);
  76. kfree(buf);
  77. len = smd_read_avail(hsmd->fm_channel);
  78. }
  79. }
  80. static int radio_hci_smd_send_frame(struct sk_buff *skb)
  81. {
  82. int len = 0;
  83. len = smd_write(hs.fm_channel, skb->data, skb->len);
  84. if (len < skb->len) {
  85. FMDERR("Failed to write Data %d", len);
  86. kfree_skb(skb);
  87. return -ENODEV;
  88. }
  89. kfree_skb(skb);
  90. return 0;
  91. }
  92. static void send_disable_event(struct work_struct *worker)
  93. {
  94. struct sk_buff *skb;
  95. unsigned char buf[6] = { 0x0f, 0x04, 0x01, 0x02, 0x4c, 0x00 };
  96. int len = sizeof(buf);
  97. skb = alloc_skb(len, GFP_ATOMIC);
  98. if (!skb) {
  99. FMDERR("Memory not allocated for the socket");
  100. kfree(worker);
  101. return;
  102. }
  103. FMDERR("FM INSERT DISABLE Rx Event");
  104. memcpy(skb_put(skb, len), buf, len);
  105. skb_orphan(skb);
  106. skb->dev = (struct net_device *)hs.hdev;
  107. radio_hci_recv_frame(skb);
  108. kfree(worker);
  109. }
  110. static void radio_hci_smd_notify_cmd(void *data, unsigned int event)
  111. {
  112. struct radio_hci_dev *hdev = hs.hdev;
  113. if (!hdev) {
  114. FMDERR("Frame for unknown HCI device (hdev=NULL)");
  115. return;
  116. }
  117. switch (event) {
  118. case SMD_EVENT_DATA:
  119. tasklet_schedule(&hs.rx_task);
  120. break;
  121. case SMD_EVENT_OPEN:
  122. break;
  123. case SMD_EVENT_CLOSE:
  124. reset_worker = kzalloc(sizeof(*reset_worker), GFP_ATOMIC);
  125. if (!reset_worker) {
  126. FMDERR("Out of memory");
  127. break;
  128. }
  129. INIT_WORK(reset_worker, send_disable_event);
  130. schedule_work(reset_worker);
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. static int radio_hci_smd_register_dev(struct radio_data *hsmd)
  137. {
  138. struct radio_hci_dev *hdev;
  139. int rc;
  140. if (hsmd == NULL)
  141. return -ENODEV;
  142. hdev = kmalloc(sizeof(struct radio_hci_dev), GFP_KERNEL);
  143. if (hdev == NULL)
  144. return -ENODEV;
  145. tasklet_init(&hsmd->rx_task, radio_hci_smd_recv_event,
  146. (unsigned long) hsmd);
  147. hdev->send = radio_hci_smd_send_frame;
  148. hdev->destruct = radio_hci_smd_destruct;
  149. hdev->close_smd = radio_hci_smd_exit;
  150. /* Open the SMD Channel and device and register the callback function */
  151. rc = smd_named_open_on_edge("APPS_FM", SMD_APPS_WCNSS,
  152. &hsmd->fm_channel, hdev, radio_hci_smd_notify_cmd);
  153. if (rc < 0) {
  154. FMDERR("Cannot open the command channel");
  155. hsmd->hdev = NULL;
  156. kfree(hdev);
  157. return -ENODEV;
  158. }
  159. smd_disable_read_intr(hsmd->fm_channel);
  160. if (radio_hci_register_dev(hdev) < 0) {
  161. FMDERR("Can't register HCI device");
  162. smd_close(hsmd->fm_channel);
  163. hsmd->hdev = NULL;
  164. kfree(hdev);
  165. return -ENODEV;
  166. }
  167. hsmd->hdev = hdev;
  168. return 0;
  169. }
  170. static void radio_hci_smd_deregister(void)
  171. {
  172. radio_hci_unregister_dev();
  173. kfree(hs.hdev);
  174. hs.hdev = NULL;
  175. smd_close(hs.fm_channel);
  176. hs.fm_channel = 0;
  177. fmsmd_set = 0;
  178. }
  179. static int radio_hci_smd_init(void)
  180. {
  181. int ret;
  182. if (chan_opened) {
  183. FMDBG("Channel is already opened");
  184. return 0;
  185. }
  186. /* this should be called with fm_smd_enable lock held */
  187. ret = radio_hci_smd_register_dev(&hs);
  188. if (ret < 0) {
  189. FMDERR("Failed to register smd device");
  190. chan_opened = false;
  191. return ret;
  192. }
  193. chan_opened = true;
  194. return ret;
  195. }
  196. static void radio_hci_smd_exit(void)
  197. {
  198. if (!chan_opened) {
  199. FMDBG("Channel already closed");
  200. return;
  201. }
  202. /* this should be called with fm_smd_enable lock held */
  203. radio_hci_smd_deregister();
  204. chan_opened = false;
  205. }
  206. int hci_fm_smd_register(void) {
  207. return radio_hci_smd_register_dev(&hs);
  208. }
  209. void hci_fm_smd_deregister(void) {
  210. radio_hci_smd_deregister();
  211. }
  212. static int hcismd_fm_set_enable(const char *val, struct kernel_param *kp)
  213. {
  214. int ret = 0;
  215. mutex_lock(&fm_smd_enable);
  216. ret = param_set_int(val, kp);
  217. if (ret)
  218. goto done;
  219. switch (fmsmd_set) {
  220. case 1:
  221. radio_hci_smd_init();
  222. break;
  223. case 0:
  224. radio_hci_smd_exit();
  225. break;
  226. default:
  227. ret = -EFAULT;
  228. }
  229. done:
  230. mutex_unlock(&fm_smd_enable);
  231. return ret;
  232. }
  233. MODULE_DESCRIPTION("FM SMD driver");
  234. MODULE_AUTHOR("Ankur Nandwani <ankurn@codeaurora.org>");
  235. MODULE_LICENSE("GPL v2");