btwilink.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #define DEBUG
  26. #include <linux/platform_device.h>
  27. #include <net/bluetooth/bluetooth.h>
  28. #include <net/bluetooth/hci_core.h>
  29. #include <net/bluetooth/hci.h>
  30. #include <linux/ti_wilink_st.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. skb->dev = (void *) lhst->hdev;
  96. /* Forward skb to HCI core layer */
  97. err = hci_recv_frame(skb);
  98. if (err < 0) {
  99. BT_ERR("Unable to push skb to HCI core(%d)", err);
  100. return err;
  101. }
  102. lhst->hdev->stat.byte_rx += skb->len;
  103. return 0;
  104. }
  105. /* ------- Interfaces to HCI layer ------ */
  106. /* protocol structure registered with shared transport */
  107. static struct st_proto_s ti_st_proto[MAX_BT_CHNL_IDS] = {
  108. {
  109. .chnl_id = HCI_ACLDATA_PKT, /* ACL */
  110. .hdr_len = sizeof(struct hci_acl_hdr),
  111. .offset_len_in_hdr = offsetof(struct hci_acl_hdr, dlen),
  112. .len_size = 2, /* sizeof(dlen) in struct hci_acl_hdr */
  113. .reserve = 8,
  114. },
  115. {
  116. .chnl_id = HCI_SCODATA_PKT, /* SCO */
  117. .hdr_len = sizeof(struct hci_sco_hdr),
  118. .offset_len_in_hdr = offsetof(struct hci_sco_hdr, dlen),
  119. .len_size = 1, /* sizeof(dlen) in struct hci_sco_hdr */
  120. .reserve = 8,
  121. },
  122. {
  123. .chnl_id = HCI_EVENT_PKT, /* HCI Events */
  124. .hdr_len = sizeof(struct hci_event_hdr),
  125. .offset_len_in_hdr = offsetof(struct hci_event_hdr, plen),
  126. .len_size = 1, /* sizeof(plen) in struct hci_event_hdr */
  127. .reserve = 8,
  128. },
  129. };
  130. /* Called from HCI core to initialize the device */
  131. static int ti_st_open(struct hci_dev *hdev)
  132. {
  133. unsigned long timeleft;
  134. struct ti_st *hst;
  135. int err, i;
  136. BT_DBG("%s %p", hdev->name, hdev);
  137. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  138. return -EBUSY;
  139. /* provide contexts for callbacks from ST */
  140. hst = hdev->driver_data;
  141. for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
  142. ti_st_proto[i].priv_data = hst;
  143. ti_st_proto[i].max_frame_size = HCI_MAX_FRAME_SIZE;
  144. ti_st_proto[i].recv = st_receive;
  145. ti_st_proto[i].reg_complete_cb = st_reg_completion_cb;
  146. /* Prepare wait-for-completion handler */
  147. init_completion(&hst->wait_reg_completion);
  148. /* Reset ST registration callback status flag,
  149. * this value will be updated in
  150. * st_reg_completion_cb()
  151. * function whenever it called from ST driver.
  152. */
  153. hst->reg_status = -EINPROGRESS;
  154. err = st_register(&ti_st_proto[i]);
  155. if (!err)
  156. goto done;
  157. if (err != -EINPROGRESS) {
  158. clear_bit(HCI_RUNNING, &hdev->flags);
  159. BT_ERR("st_register failed %d", err);
  160. return err;
  161. }
  162. /* ST is busy with either protocol
  163. * registration or firmware download.
  164. */
  165. BT_DBG("waiting for registration "
  166. "completion signal from ST");
  167. timeleft = wait_for_completion_timeout
  168. (&hst->wait_reg_completion,
  169. msecs_to_jiffies(BT_REGISTER_TIMEOUT));
  170. if (!timeleft) {
  171. clear_bit(HCI_RUNNING, &hdev->flags);
  172. BT_ERR("Timeout(%d sec),didn't get reg "
  173. "completion signal from ST",
  174. BT_REGISTER_TIMEOUT / 1000);
  175. return -ETIMEDOUT;
  176. }
  177. /* Is ST registration callback
  178. * called with ERROR status? */
  179. if (hst->reg_status != 0) {
  180. clear_bit(HCI_RUNNING, &hdev->flags);
  181. BT_ERR("ST registration completed with invalid "
  182. "status %d", hst->reg_status);
  183. return -EAGAIN;
  184. }
  185. done:
  186. hst->st_write = ti_st_proto[i].write;
  187. if (!hst->st_write) {
  188. BT_ERR("undefined ST write function");
  189. clear_bit(HCI_RUNNING, &hdev->flags);
  190. for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
  191. /* Undo registration with ST */
  192. err = st_unregister(&ti_st_proto[i]);
  193. if (err)
  194. BT_ERR("st_unregister() failed with "
  195. "error %d", err);
  196. hst->st_write = NULL;
  197. }
  198. return -EIO;
  199. }
  200. }
  201. return 0;
  202. }
  203. /* Close device */
  204. static int ti_st_close(struct hci_dev *hdev)
  205. {
  206. int err, i;
  207. struct ti_st *hst = hdev->driver_data;
  208. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  209. return 0;
  210. for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
  211. err = st_unregister(&ti_st_proto[i]);
  212. if (err)
  213. BT_ERR("st_unregister(%d) failed with error %d",
  214. ti_st_proto[i].chnl_id, err);
  215. }
  216. hst->st_write = NULL;
  217. return err;
  218. }
  219. static int ti_st_send_frame(struct sk_buff *skb)
  220. {
  221. struct hci_dev *hdev;
  222. struct ti_st *hst;
  223. long len;
  224. hdev = (struct hci_dev *)skb->dev;
  225. if (!test_bit(HCI_RUNNING, &hdev->flags))
  226. return -EBUSY;
  227. hst = hdev->driver_data;
  228. /* Prepend skb with frame type */
  229. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  230. BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
  231. skb->len);
  232. /* Insert skb to shared transport layer's transmit queue.
  233. * Freeing skb memory is taken care in shared transport layer,
  234. * so don't free skb memory here.
  235. */
  236. len = hst->st_write(skb);
  237. if (len < 0) {
  238. kfree_skb(skb);
  239. BT_ERR("ST write failed (%ld)", len);
  240. /* Try Again, would only fail if UART has gone bad */
  241. return -EAGAIN;
  242. }
  243. /* ST accepted our skb. So, Go ahead and do rest */
  244. hdev->stat.byte_tx += len;
  245. ti_st_tx_complete(hst, bt_cb(skb)->pkt_type);
  246. return 0;
  247. }
  248. static void ti_st_destruct(struct hci_dev *hdev)
  249. {
  250. BT_DBG("%s", hdev->name);
  251. /* do nothing here, since platform remove
  252. * would free the hdev->driver_data
  253. */
  254. }
  255. static int bt_ti_probe(struct platform_device *pdev)
  256. {
  257. static struct ti_st *hst;
  258. struct hci_dev *hdev;
  259. int err;
  260. hst = kzalloc(sizeof(struct ti_st), GFP_KERNEL);
  261. if (!hst)
  262. return -ENOMEM;
  263. /* Expose "hciX" device to user space */
  264. hdev = hci_alloc_dev();
  265. if (!hdev) {
  266. kfree(hst);
  267. return -ENOMEM;
  268. }
  269. BT_DBG("hdev %p", hdev);
  270. hst->hdev = hdev;
  271. hdev->bus = HCI_UART;
  272. hdev->driver_data = hst;
  273. hdev->open = ti_st_open;
  274. hdev->close = ti_st_close;
  275. hdev->flush = NULL;
  276. hdev->send = ti_st_send_frame;
  277. hdev->destruct = ti_st_destruct;
  278. hdev->owner = THIS_MODULE;
  279. err = hci_register_dev(hdev);
  280. if (err < 0) {
  281. BT_ERR("Can't register HCI device error %d", err);
  282. kfree(hst);
  283. hci_free_dev(hdev);
  284. return err;
  285. }
  286. BT_DBG("HCI device registered (hdev %p)", hdev);
  287. dev_set_drvdata(&pdev->dev, hst);
  288. return err;
  289. }
  290. static int bt_ti_remove(struct platform_device *pdev)
  291. {
  292. struct hci_dev *hdev;
  293. struct ti_st *hst = dev_get_drvdata(&pdev->dev);
  294. if (!hst)
  295. return -EFAULT;
  296. BT_DBG("%s", hst->hdev->name);
  297. hdev = hst->hdev;
  298. ti_st_close(hdev);
  299. hci_unregister_dev(hdev);
  300. hci_free_dev(hdev);
  301. kfree(hst);
  302. dev_set_drvdata(&pdev->dev, NULL);
  303. return 0;
  304. }
  305. static struct platform_driver btwilink_driver = {
  306. .probe = bt_ti_probe,
  307. .remove = bt_ti_remove,
  308. .driver = {
  309. .name = "btwilink",
  310. .owner = THIS_MODULE,
  311. },
  312. };
  313. /* ------- Module Init/Exit interfaces ------ */
  314. static int __init btwilink_init(void)
  315. {
  316. BT_INFO("Bluetooth Driver for TI WiLink - Version %s", VERSION);
  317. return platform_driver_register(&btwilink_driver);
  318. }
  319. static void __exit btwilink_exit(void)
  320. {
  321. platform_driver_unregister(&btwilink_driver);
  322. }
  323. module_init(btwilink_init);
  324. module_exit(btwilink_exit);
  325. /* ------ Module Info ------ */
  326. MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
  327. MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
  328. MODULE_VERSION(VERSION);
  329. MODULE_LICENSE("GPL");