radio-iris-transport.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. static DEFINE_MUTEX(fm_smd_enable);
  40. static int fmsmd_set;
  41. static int hcismd_fm_set_enable(const char *val, struct kernel_param *kp);
  42. module_param_call(fmsmd_set, hcismd_fm_set_enable, NULL, &fmsmd_set, 0644);
  43. static struct work_struct *reset_worker;
  44. static void radio_hci_smd_deregister(void);
  45. static void radio_hci_smd_destruct(struct radio_hci_dev *hdev)
  46. {
  47. radio_hci_unregister_dev(hs.hdev);
  48. }
  49. static void radio_hci_smd_recv_event(unsigned long temp)
  50. {
  51. int len;
  52. int rc;
  53. struct sk_buff *skb;
  54. unsigned char *buf;
  55. struct radio_data *hsmd = &hs;
  56. len = smd_read_avail(hsmd->fm_channel);
  57. while (len) {
  58. skb = alloc_skb(len, GFP_ATOMIC);
  59. if (!skb) {
  60. FMDERR("Memory not allocated for the socket");
  61. return;
  62. }
  63. buf = kmalloc(len, GFP_ATOMIC);
  64. if (!buf) {
  65. kfree_skb(skb);
  66. FMDERR("Error in allocating buffer memory");
  67. return;
  68. }
  69. rc = smd_read(hsmd->fm_channel, (void *)buf, len);
  70. memcpy(skb_put(skb, len), buf, len);
  71. skb_orphan(skb);
  72. skb->dev = (struct net_device *)hs.hdev;
  73. rc = radio_hci_recv_frame(skb);
  74. kfree(buf);
  75. len = smd_read_avail(hsmd->fm_channel);
  76. }
  77. }
  78. static int radio_hci_smd_send_frame(struct sk_buff *skb)
  79. {
  80. int len = 0;
  81. len = smd_write(hs.fm_channel, skb->data, skb->len);
  82. if (len < skb->len) {
  83. FMDERR("Failed to write Data %d", len);
  84. kfree_skb(skb);
  85. return -ENODEV;
  86. }
  87. kfree_skb(skb);
  88. return 0;
  89. }
  90. static void send_disable_event(struct work_struct *worker)
  91. {
  92. struct sk_buff *skb;
  93. unsigned char buf[6] = { 0x0f, 0x04, 0x01, 0x02, 0x4c, 0x00 };
  94. int len = sizeof(buf);
  95. skb = alloc_skb(len, GFP_ATOMIC);
  96. if (!skb) {
  97. FMDERR("Memory not allocated for the socket");
  98. kfree(worker);
  99. return;
  100. }
  101. FMDERR("FM INSERT DISABLE Rx Event");
  102. memcpy(skb_put(skb, len), buf, len);
  103. skb_orphan(skb);
  104. skb->dev = (struct net_device *)hs.hdev;
  105. radio_hci_recv_frame(skb);
  106. kfree(worker);
  107. }
  108. static void radio_hci_smd_notify_cmd(void *data, unsigned int event)
  109. {
  110. struct radio_hci_dev *hdev = hs.hdev;
  111. if (!hdev) {
  112. FMDERR("Frame for unknown HCI device (hdev=NULL)");
  113. return;
  114. }
  115. switch (event) {
  116. case SMD_EVENT_DATA:
  117. tasklet_schedule(&hs.rx_task);
  118. break;
  119. case SMD_EVENT_OPEN:
  120. break;
  121. case SMD_EVENT_CLOSE:
  122. reset_worker = kzalloc(sizeof(*reset_worker), GFP_ATOMIC);
  123. if (!reset_worker) {
  124. FMDERR("Out of memory");
  125. break;
  126. }
  127. INIT_WORK(reset_worker, send_disable_event);
  128. schedule_work(reset_worker);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. static int radio_hci_smd_register_dev(struct radio_data *hsmd)
  135. {
  136. struct radio_hci_dev *hdev;
  137. int rc;
  138. if (hsmd == NULL)
  139. return -ENODEV;
  140. hdev = kmalloc(sizeof(struct radio_hci_dev), GFP_KERNEL);
  141. if (hdev == NULL)
  142. return -ENODEV;
  143. hsmd->hdev = hdev;
  144. tasklet_init(&hsmd->rx_task, radio_hci_smd_recv_event,
  145. (unsigned long) hsmd);
  146. hdev->send = radio_hci_smd_send_frame;
  147. hdev->destruct = radio_hci_smd_destruct;
  148. hdev->close_smd = radio_hci_smd_deregister;
  149. /* Open the SMD Channel and device and register the callback function */
  150. rc = smd_named_open_on_edge("APPS_FM", SMD_APPS_WCNSS,
  151. &hsmd->fm_channel, hdev, radio_hci_smd_notify_cmd);
  152. if (rc < 0) {
  153. FMDERR("Cannot open the command channel");
  154. hsmd->hdev = NULL;
  155. kfree(hdev);
  156. return -ENODEV;
  157. }
  158. smd_disable_read_intr(hsmd->fm_channel);
  159. if (radio_hci_register_dev(hdev) < 0) {
  160. FMDERR("Can't register HCI device");
  161. smd_close(hsmd->fm_channel);
  162. hsmd->hdev = NULL;
  163. kfree(hdev);
  164. return -ENODEV;
  165. }
  166. return 0;
  167. }
  168. static void radio_hci_smd_deregister(void)
  169. {
  170. smd_close(hs.fm_channel);
  171. hs.fm_channel = 0;
  172. fmsmd_set = 0;
  173. }
  174. static int radio_hci_smd_init(void)
  175. {
  176. return radio_hci_smd_register_dev(&hs);
  177. }
  178. static void radio_hci_smd_exit(void)
  179. {
  180. radio_hci_smd_deregister();
  181. }
  182. static int hcismd_fm_set_enable(const char *val, struct kernel_param *kp)
  183. {
  184. int ret = 0;
  185. mutex_lock(&fm_smd_enable);
  186. ret = param_set_int(val, kp);
  187. if (ret)
  188. goto done;
  189. switch (fmsmd_set) {
  190. case 1:
  191. radio_hci_smd_init();
  192. break;
  193. case 0:
  194. radio_hci_smd_exit();
  195. break;
  196. default:
  197. ret = -EFAULT;
  198. }
  199. done:
  200. mutex_unlock(&fm_smd_enable);
  201. return ret;
  202. }
  203. MODULE_DESCRIPTION("FM SMD driver");
  204. MODULE_AUTHOR("Ankur Nandwani <ankurn@codeaurora.org>");
  205. MODULE_LICENSE("GPL v2");