btwilink.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  137. return -EBUSY;
  138. /* provide contexts for callbacks from ST */
  139. hst = hci_get_drvdata(hdev);
  140. for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
  141. ti_st_proto[i].priv_data = hst;
  142. ti_st_proto[i].max_frame_size = HCI_MAX_FRAME_SIZE;
  143. ti_st_proto[i].recv = st_receive;
  144. ti_st_proto[i].reg_complete_cb = st_reg_completion_cb;
  145. /* Prepare wait-for-completion handler */
  146. init_completion(&hst->wait_reg_completion);
  147. /* Reset ST registration callback status flag,
  148. * this value will be updated in
  149. * st_reg_completion_cb()
  150. * function whenever it called from ST driver.
  151. */
  152. hst->reg_status = -EINPROGRESS;
  153. err = st_register(&ti_st_proto[i]);
  154. if (!err)
  155. goto done;
  156. if (err != -EINPROGRESS) {
  157. clear_bit(HCI_RUNNING, &hdev->flags);
  158. BT_ERR("st_register failed %d", err);
  159. return err;
  160. }
  161. /* ST is busy with either protocol
  162. * registration or firmware download.
  163. */
  164. BT_DBG("waiting for registration "
  165. "completion signal from ST");
  166. timeleft = wait_for_completion_timeout
  167. (&hst->wait_reg_completion,
  168. msecs_to_jiffies(BT_REGISTER_TIMEOUT));
  169. if (!timeleft) {
  170. clear_bit(HCI_RUNNING, &hdev->flags);
  171. BT_ERR("Timeout(%d sec),didn't get reg "
  172. "completion signal from ST",
  173. BT_REGISTER_TIMEOUT / 1000);
  174. return -ETIMEDOUT;
  175. }
  176. /* Is ST registration callback
  177. * called with ERROR status? */
  178. if (hst->reg_status != 0) {
  179. clear_bit(HCI_RUNNING, &hdev->flags);
  180. BT_ERR("ST registration completed with invalid "
  181. "status %d", hst->reg_status);
  182. return -EAGAIN;
  183. }
  184. done:
  185. hst->st_write = ti_st_proto[i].write;
  186. if (!hst->st_write) {
  187. BT_ERR("undefined ST write function");
  188. clear_bit(HCI_RUNNING, &hdev->flags);
  189. for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
  190. /* Undo registration with ST */
  191. err = st_unregister(&ti_st_proto[i]);
  192. if (err)
  193. BT_ERR("st_unregister() failed with "
  194. "error %d", err);
  195. hst->st_write = NULL;
  196. }
  197. return -EIO;
  198. }
  199. }
  200. return 0;
  201. }
  202. /* Close device */
  203. static int ti_st_close(struct hci_dev *hdev)
  204. {
  205. int err, i;
  206. struct ti_st *hst = hci_get_drvdata(hdev);
  207. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  208. return 0;
  209. for (i = MAX_BT_CHNL_IDS-1; i >= 0; i--) {
  210. err = st_unregister(&ti_st_proto[i]);
  211. if (err)
  212. BT_ERR("st_unregister(%d) failed with error %d",
  213. ti_st_proto[i].chnl_id, err);
  214. }
  215. hst->st_write = NULL;
  216. return err;
  217. }
  218. static int ti_st_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  219. {
  220. struct ti_st *hst;
  221. long len;
  222. if (!test_bit(HCI_RUNNING, &hdev->flags))
  223. return -EBUSY;
  224. hst = hci_get_drvdata(hdev);
  225. /* Prepend skb with frame type */
  226. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  227. BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
  228. skb->len);
  229. /* Insert skb to shared transport layer's transmit queue.
  230. * Freeing skb memory is taken care in shared transport layer,
  231. * so don't free skb memory here.
  232. */
  233. len = hst->st_write(skb);
  234. if (len < 0) {
  235. kfree_skb(skb);
  236. BT_ERR("ST write failed (%ld)", len);
  237. /* Try Again, would only fail if UART has gone bad */
  238. return -EAGAIN;
  239. }
  240. /* ST accepted our skb. So, Go ahead and do rest */
  241. hdev->stat.byte_tx += len;
  242. ti_st_tx_complete(hst, bt_cb(skb)->pkt_type);
  243. return 0;
  244. }
  245. static int bt_ti_probe(struct platform_device *pdev)
  246. {
  247. static struct ti_st *hst;
  248. struct hci_dev *hdev;
  249. int err;
  250. hst = devm_kzalloc(&pdev->dev, sizeof(struct ti_st), GFP_KERNEL);
  251. if (!hst)
  252. return -ENOMEM;
  253. /* Expose "hciX" device to user space */
  254. hdev = hci_alloc_dev();
  255. if (!hdev)
  256. return -ENOMEM;
  257. BT_DBG("hdev %p", hdev);
  258. hst->hdev = hdev;
  259. hdev->bus = HCI_UART;
  260. hci_set_drvdata(hdev, hst);
  261. hdev->open = ti_st_open;
  262. hdev->close = ti_st_close;
  263. hdev->flush = NULL;
  264. hdev->send = ti_st_send_frame;
  265. err = hci_register_dev(hdev);
  266. if (err < 0) {
  267. BT_ERR("Can't register HCI device error %d", err);
  268. hci_free_dev(hdev);
  269. return err;
  270. }
  271. BT_DBG("HCI device registered (hdev %p)", hdev);
  272. dev_set_drvdata(&pdev->dev, hst);
  273. return err;
  274. }
  275. static int bt_ti_remove(struct platform_device *pdev)
  276. {
  277. struct hci_dev *hdev;
  278. struct ti_st *hst = dev_get_drvdata(&pdev->dev);
  279. if (!hst)
  280. return -EFAULT;
  281. BT_DBG("%s", hst->hdev->name);
  282. hdev = hst->hdev;
  283. ti_st_close(hdev);
  284. hci_unregister_dev(hdev);
  285. hci_free_dev(hdev);
  286. dev_set_drvdata(&pdev->dev, NULL);
  287. return 0;
  288. }
  289. static struct platform_driver btwilink_driver = {
  290. .probe = bt_ti_probe,
  291. .remove = bt_ti_remove,
  292. .driver = {
  293. .name = "btwilink",
  294. },
  295. };
  296. module_platform_driver(btwilink_driver);
  297. /* ------ Module Info ------ */
  298. MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
  299. MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
  300. MODULE_VERSION(VERSION);
  301. MODULE_LICENSE("GPL");