btwilink.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Texas Instrument's Bluetooth Driver For Shared Transport.
  3. *
  4. * Bluetooth Driver acts as interface between HCI core and
  5. * TI Shared Transport Layer.
  6. *
  7. * Copyright (C) 2009-2010 Texas Instruments
  8. * Author: Raja Mani <raja_mani@ti.com>
  9. * Pavan Savoy <pavan_savoy@ti.com>
  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 as
  13. * 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. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/platform_device.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include <net/bluetooth/hci.h>
  29. #include <linux/ti_wilink_st.h>
  30. #include <linux/module.h>
  31. /* Bluetooth Driver Version */
  32. #define VERSION "1.0"
  33. #define MAX_BT_CHNL_IDS 3
  34. /* Number of seconds to wait for registration completion
  35. * when ST returns PENDING status.
  36. */
  37. #define BT_REGISTER_TIMEOUT 6000 /* 6 sec */
  38. /**
  39. * struct ti_st - driver operation structure
  40. * @hdev: hci device pointer which binds to bt driver
  41. * @reg_status: ST registration callback status
  42. * @st_write: write function provided by the ST driver
  43. * to be used by the driver during send_frame.
  44. * @wait_reg_completion - completion sync between ti_st_open
  45. * and st_reg_completion_cb.
  46. */
  47. struct ti_st {
  48. struct hci_dev *hdev;
  49. int reg_status;
  50. long (*st_write) (struct sk_buff *);
  51. struct completion wait_reg_completion;
  52. };
  53. /* Increments HCI counters based on pocket ID (cmd,acl,sco) */
  54. static inline void ti_st_tx_complete(struct ti_st *hst, int pkt_type)
  55. {
  56. struct hci_dev *hdev = hst->hdev;
  57. /* Update HCI stat counters */
  58. switch (pkt_type) {
  59. case HCI_COMMAND_PKT:
  60. hdev->stat.cmd_tx++;
  61. break;
  62. case HCI_ACLDATA_PKT:
  63. hdev->stat.acl_tx++;
  64. break;
  65. case HCI_SCODATA_PKT:
  66. hdev->stat.sco_tx++;
  67. break;
  68. }
  69. }
  70. /* ------- Interfaces to Shared Transport ------ */
  71. /* Called by ST layer to indicate protocol registration completion
  72. * status.ti_st_open() function will wait for signal from this
  73. * API when st_register() function returns ST_PENDING.
  74. */
  75. static void st_reg_completion_cb(void *priv_data, int data)
  76. {
  77. struct ti_st *lhst = priv_data;
  78. /* Save registration status for use in ti_st_open() */
  79. lhst->reg_status = data;
  80. /* complete the wait in ti_st_open() */
  81. complete(&lhst->wait_reg_completion);
  82. }
  83. /* Called by Shared Transport layer when receive data is available */
  84. static long st_receive(void *priv_data, struct sk_buff *skb)
  85. {
  86. struct ti_st *lhst = priv_data;
  87. int err;
  88. if (!skb)
  89. return -EFAULT;
  90. if (!lhst) {
  91. kfree_skb(skb);
  92. return -EFAULT;
  93. }
  94. /* Forward skb to HCI core layer */
  95. err = hci_recv_frame(lhst->hdev, skb);
  96. if (err < 0) {
  97. BT_ERR("Unable to push skb to HCI core(%d)", err);
  98. return err;
  99. }
  100. lhst->hdev->stat.byte_rx += skb->len;
  101. return 0;
  102. }
  103. /* ------- Interfaces to HCI layer ------ */
  104. /* protocol structure registered with shared transport */
  105. static struct st_proto_s ti_st_proto[MAX_BT_CHNL_IDS] = {
  106. {
  107. .chnl_id = HCI_EVENT_PKT, /* HCI Events */
  108. .hdr_len = sizeof(struct hci_event_hdr),
  109. .offset_len_in_hdr = offsetof(struct hci_event_hdr, plen),
  110. .len_size = 1, /* sizeof(plen) in struct hci_event_hdr */
  111. .reserve = 8,
  112. },
  113. {
  114. .chnl_id = HCI_ACLDATA_PKT, /* ACL */
  115. .hdr_len = sizeof(struct hci_acl_hdr),
  116. .offset_len_in_hdr = offsetof(struct hci_acl_hdr, dlen),
  117. .len_size = 2, /* sizeof(dlen) in struct hci_acl_hdr */
  118. .reserve = 8,
  119. },
  120. {
  121. .chnl_id = HCI_SCODATA_PKT, /* SCO */
  122. .hdr_len = sizeof(struct hci_sco_hdr),
  123. .offset_len_in_hdr = offsetof(struct hci_sco_hdr, dlen),
  124. .len_size = 1, /* sizeof(dlen) in struct hci_sco_hdr */
  125. .reserve = 8,
  126. },
  127. };
  128. /* Called from HCI core to initialize the device */
  129. static int ti_st_open(struct hci_dev *hdev)
  130. {
  131. unsigned long timeleft;
  132. struct ti_st *hst;
  133. int err, i;
  134. BT_DBG("%s %p", hdev->name, hdev);
  135. /* provide contexts for callbacks from ST */
  136. hst = hci_get_drvdata(hdev);
  137. for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
  138. ti_st_proto[i].priv_data = hst;
  139. ti_st_proto[i].max_frame_size = HCI_MAX_FRAME_SIZE;
  140. ti_st_proto[i].recv = st_receive;
  141. ti_st_proto[i].reg_complete_cb = st_reg_completion_cb;
  142. /* Prepare wait-for-completion handler */
  143. init_completion(&hst->wait_reg_completion);
  144. /* Reset ST registration callback status flag,
  145. * this value will be updated in
  146. * st_reg_completion_cb()
  147. * function whenever it called from ST driver.
  148. */
  149. hst->reg_status = -EINPROGRESS;
  150. err = st_register(&ti_st_proto[i]);
  151. if (!err)
  152. goto done;
  153. if (err != -EINPROGRESS) {
  154. BT_ERR("st_register failed %d", err);
  155. return err;
  156. }
  157. /* ST is busy with either protocol
  158. * registration or firmware download.
  159. */
  160. BT_DBG("waiting for registration "
  161. "completion signal from ST");
  162. timeleft = wait_for_completion_timeout
  163. (&hst->wait_reg_completion,
  164. msecs_to_jiffies(BT_REGISTER_TIMEOUT));
  165. if (!timeleft) {
  166. BT_ERR("Timeout(%d sec),didn't get reg "
  167. "completion signal from ST",
  168. BT_REGISTER_TIMEOUT / 1000);
  169. return -ETIMEDOUT;
  170. }
  171. /* Is ST registration callback
  172. * called with ERROR status?
  173. */
  174. if (hst->reg_status != 0) {
  175. BT_ERR("ST registration completed with invalid "
  176. "status %d", hst->reg_status);
  177. return -EAGAIN;
  178. }
  179. done:
  180. hst->st_write = ti_st_proto[i].write;
  181. if (!hst->st_write) {
  182. BT_ERR("undefined ST write function");
  183. for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
  184. /* Undo registration with ST */
  185. err = st_unregister(&ti_st_proto[i]);
  186. if (err)
  187. BT_ERR("st_unregister() failed with "
  188. "error %d", err);
  189. hst->st_write = NULL;
  190. }
  191. return -EIO;
  192. }
  193. }
  194. return 0;
  195. }
  196. /* Close device */
  197. static int ti_st_close(struct hci_dev *hdev)
  198. {
  199. int err, i;
  200. struct ti_st *hst = hci_get_drvdata(hdev);
  201. for (i = MAX_BT_CHNL_IDS-1; i >= 0; i--) {
  202. err = st_unregister(&ti_st_proto[i]);
  203. if (err)
  204. BT_ERR("st_unregister(%d) failed with error %d",
  205. ti_st_proto[i].chnl_id, err);
  206. }
  207. hst->st_write = NULL;
  208. return err;
  209. }
  210. static int ti_st_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  211. {
  212. struct ti_st *hst;
  213. long len;
  214. int pkt_type;
  215. hst = hci_get_drvdata(hdev);
  216. /* Prepend skb with frame type */
  217. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  218. BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
  219. skb->len);
  220. /* Insert skb to shared transport layer's transmit queue.
  221. * Freeing skb memory is taken care in shared transport layer,
  222. * so don't free skb memory here.
  223. */
  224. pkt_type = hci_skb_pkt_type(skb);
  225. len = hst->st_write(skb);
  226. if (len < 0) {
  227. BT_ERR("ST write failed (%ld)", len);
  228. /* Try Again, would only fail if UART has gone bad */
  229. return -EAGAIN;
  230. }
  231. /* ST accepted our skb. So, Go ahead and do rest */
  232. hdev->stat.byte_tx += len;
  233. ti_st_tx_complete(hst, pkt_type);
  234. return 0;
  235. }
  236. static int bt_ti_probe(struct platform_device *pdev)
  237. {
  238. struct ti_st *hst;
  239. struct hci_dev *hdev;
  240. int err;
  241. hst = devm_kzalloc(&pdev->dev, sizeof(struct ti_st), GFP_KERNEL);
  242. if (!hst)
  243. return -ENOMEM;
  244. /* Expose "hciX" device to user space */
  245. hdev = hci_alloc_dev();
  246. if (!hdev)
  247. return -ENOMEM;
  248. BT_DBG("hdev %p", hdev);
  249. hst->hdev = hdev;
  250. hdev->bus = HCI_UART;
  251. hci_set_drvdata(hdev, hst);
  252. hdev->open = ti_st_open;
  253. hdev->close = ti_st_close;
  254. hdev->flush = NULL;
  255. hdev->send = ti_st_send_frame;
  256. err = hci_register_dev(hdev);
  257. if (err < 0) {
  258. BT_ERR("Can't register HCI device error %d", err);
  259. hci_free_dev(hdev);
  260. return err;
  261. }
  262. BT_DBG("HCI device registered (hdev %p)", hdev);
  263. dev_set_drvdata(&pdev->dev, hst);
  264. return 0;
  265. }
  266. static int bt_ti_remove(struct platform_device *pdev)
  267. {
  268. struct hci_dev *hdev;
  269. struct ti_st *hst = dev_get_drvdata(&pdev->dev);
  270. if (!hst)
  271. return -EFAULT;
  272. BT_DBG("%s", hst->hdev->name);
  273. hdev = hst->hdev;
  274. ti_st_close(hdev);
  275. hci_unregister_dev(hdev);
  276. hci_free_dev(hdev);
  277. dev_set_drvdata(&pdev->dev, NULL);
  278. return 0;
  279. }
  280. static struct platform_driver btwilink_driver = {
  281. .probe = bt_ti_probe,
  282. .remove = bt_ti_remove,
  283. .driver = {
  284. .name = "btwilink",
  285. },
  286. };
  287. module_platform_driver(btwilink_driver);
  288. /* ------ Module Info ------ */
  289. MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
  290. MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
  291. MODULE_VERSION(VERSION);
  292. MODULE_LICENSE("GPL");