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