btwilink.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. char 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, char 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
  84. * available */
  85. static long st_receive(void *priv_data, struct sk_buff *skb)
  86. {
  87. struct ti_st *lhst = priv_data;
  88. int err;
  89. if (!skb)
  90. return -EFAULT;
  91. if (!lhst) {
  92. kfree_skb(skb);
  93. return -EFAULT;
  94. }
  95. /* Forward skb to HCI core layer */
  96. err = hci_recv_frame(lhst->hdev, skb);
  97. if (err < 0) {
  98. BT_ERR("Unable to push skb to HCI core(%d)", err);
  99. return err;
  100. }
  101. lhst->hdev->stat.byte_rx += skb->len;
  102. return 0;
  103. }
  104. /* ------- Interfaces to HCI layer ------ */
  105. /* protocol structure registered with shared transport */
  106. static struct st_proto_s ti_st_proto[MAX_BT_CHNL_IDS] = {
  107. {
  108. .chnl_id = HCI_EVENT_PKT, /* HCI Events */
  109. .hdr_len = sizeof(struct hci_event_hdr),
  110. .offset_len_in_hdr = offsetof(struct hci_event_hdr, plen),
  111. .len_size = 1, /* sizeof(plen) in struct hci_event_hdr */
  112. .reserve = 8,
  113. },
  114. {
  115. .chnl_id = HCI_ACLDATA_PKT, /* ACL */
  116. .hdr_len = sizeof(struct hci_acl_hdr),
  117. .offset_len_in_hdr = offsetof(struct hci_acl_hdr, dlen),
  118. .len_size = 2, /* sizeof(dlen) in struct hci_acl_hdr */
  119. .reserve = 8,
  120. },
  121. {
  122. .chnl_id = HCI_SCODATA_PKT, /* SCO */
  123. .hdr_len = sizeof(struct hci_sco_hdr),
  124. .offset_len_in_hdr = offsetof(struct hci_sco_hdr, dlen),
  125. .len_size = 1, /* sizeof(dlen) in struct hci_sco_hdr */
  126. .reserve = 8,
  127. },
  128. };
  129. /* Called from HCI core to initialize the device */
  130. static int ti_st_open(struct hci_dev *hdev)
  131. {
  132. unsigned long timeleft;
  133. struct ti_st *hst;
  134. int err, i;
  135. BT_DBG("%s %p", hdev->name, hdev);
  136. /* provide contexts for callbacks from ST */
  137. hst = hci_get_drvdata(hdev);
  138. for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
  139. ti_st_proto[i].priv_data = hst;
  140. ti_st_proto[i].max_frame_size = HCI_MAX_FRAME_SIZE;
  141. ti_st_proto[i].recv = st_receive;
  142. ti_st_proto[i].reg_complete_cb = st_reg_completion_cb;
  143. /* Prepare wait-for-completion handler */
  144. init_completion(&hst->wait_reg_completion);
  145. /* Reset ST registration callback status flag,
  146. * this value will be updated in
  147. * st_reg_completion_cb()
  148. * function whenever it called from ST driver.
  149. */
  150. hst->reg_status = -EINPROGRESS;
  151. err = st_register(&ti_st_proto[i]);
  152. if (!err)
  153. goto done;
  154. if (err != -EINPROGRESS) {
  155. BT_ERR("st_register failed %d", err);
  156. return err;
  157. }
  158. /* ST is busy with either protocol
  159. * registration or firmware download.
  160. */
  161. BT_DBG("waiting for registration "
  162. "completion signal from ST");
  163. timeleft = wait_for_completion_timeout
  164. (&hst->wait_reg_completion,
  165. msecs_to_jiffies(BT_REGISTER_TIMEOUT));
  166. if (!timeleft) {
  167. BT_ERR("Timeout(%d sec),didn't get reg "
  168. "completion signal from ST",
  169. BT_REGISTER_TIMEOUT / 1000);
  170. return -ETIMEDOUT;
  171. }
  172. /* Is ST registration callback
  173. * called with ERROR status? */
  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. hst = hci_get_drvdata(hdev);
  215. /* Prepend skb with frame type */
  216. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  217. BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
  218. skb->len);
  219. /* Insert skb to shared transport layer's transmit queue.
  220. * Freeing skb memory is taken care in shared transport layer,
  221. * so don't free skb memory here.
  222. */
  223. len = hst->st_write(skb);
  224. if (len < 0) {
  225. kfree_skb(skb);
  226. BT_ERR("ST write failed (%ld)", len);
  227. /* Try Again, would only fail if UART has gone bad */
  228. return -EAGAIN;
  229. }
  230. /* ST accepted our skb. So, Go ahead and do rest */
  231. hdev->stat.byte_tx += len;
  232. ti_st_tx_complete(hst, bt_cb(skb)->pkt_type);
  233. return 0;
  234. }
  235. static int bt_ti_probe(struct platform_device *pdev)
  236. {
  237. static struct ti_st *hst;
  238. struct hci_dev *hdev;
  239. int err;
  240. hst = devm_kzalloc(&pdev->dev, sizeof(struct ti_st), GFP_KERNEL);
  241. if (!hst)
  242. return -ENOMEM;
  243. /* Expose "hciX" device to user space */
  244. hdev = hci_alloc_dev();
  245. if (!hdev)
  246. return -ENOMEM;
  247. BT_DBG("hdev %p", hdev);
  248. hst->hdev = hdev;
  249. hdev->bus = HCI_UART;
  250. hci_set_drvdata(hdev, hst);
  251. hdev->open = ti_st_open;
  252. hdev->close = ti_st_close;
  253. hdev->flush = NULL;
  254. hdev->send = ti_st_send_frame;
  255. err = hci_register_dev(hdev);
  256. if (err < 0) {
  257. BT_ERR("Can't register HCI device error %d", err);
  258. hci_free_dev(hdev);
  259. return err;
  260. }
  261. BT_DBG("HCI device registered (hdev %p)", hdev);
  262. dev_set_drvdata(&pdev->dev, hst);
  263. return err;
  264. }
  265. static int bt_ti_remove(struct platform_device *pdev)
  266. {
  267. struct hci_dev *hdev;
  268. struct ti_st *hst = dev_get_drvdata(&pdev->dev);
  269. if (!hst)
  270. return -EFAULT;
  271. BT_DBG("%s", hst->hdev->name);
  272. hdev = hst->hdev;
  273. ti_st_close(hdev);
  274. hci_unregister_dev(hdev);
  275. hci_free_dev(hdev);
  276. dev_set_drvdata(&pdev->dev, NULL);
  277. return 0;
  278. }
  279. static struct platform_driver btwilink_driver = {
  280. .probe = bt_ti_probe,
  281. .remove = bt_ti_remove,
  282. .driver = {
  283. .name = "btwilink",
  284. },
  285. };
  286. module_platform_driver(btwilink_driver);
  287. /* ------ Module Info ------ */
  288. MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
  289. MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
  290. MODULE_VERSION(VERSION);
  291. MODULE_LICENSE("GPL");