hci_bcm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. *
  3. * Bluetooth HCI UART driver for Broadcom devices
  4. *
  5. * Copyright (C) 2015 Intel Corporation
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/firmware.h>
  27. #include <net/bluetooth/bluetooth.h>
  28. #include <net/bluetooth/hci_core.h>
  29. #include "btbcm.h"
  30. #include "hci_uart.h"
  31. struct bcm_data {
  32. struct sk_buff *rx_skb;
  33. struct sk_buff_head txq;
  34. };
  35. static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
  36. {
  37. struct hci_dev *hdev = hu->hdev;
  38. struct sk_buff *skb;
  39. struct bcm_update_uart_baud_rate param;
  40. if (speed > 3000000) {
  41. struct bcm_write_uart_clock_setting clock;
  42. clock.type = BCM_UART_CLOCK_48MHZ;
  43. BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type);
  44. /* This Broadcom specific command changes the UART's controller
  45. * clock for baud rate > 3000000.
  46. */
  47. skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
  48. if (IS_ERR(skb)) {
  49. int err = PTR_ERR(skb);
  50. BT_ERR("%s: BCM: failed to write clock command (%d)",
  51. hdev->name, err);
  52. return err;
  53. }
  54. kfree_skb(skb);
  55. }
  56. BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed);
  57. param.zero = cpu_to_le16(0);
  58. param.baud_rate = cpu_to_le32(speed);
  59. /* This Broadcom specific command changes the UART's controller baud
  60. * rate.
  61. */
  62. skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
  63. HCI_INIT_TIMEOUT);
  64. if (IS_ERR(skb)) {
  65. int err = PTR_ERR(skb);
  66. BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
  67. hdev->name, err);
  68. return err;
  69. }
  70. kfree_skb(skb);
  71. return 0;
  72. }
  73. static int bcm_open(struct hci_uart *hu)
  74. {
  75. struct bcm_data *bcm;
  76. BT_DBG("hu %p", hu);
  77. bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
  78. if (!bcm)
  79. return -ENOMEM;
  80. skb_queue_head_init(&bcm->txq);
  81. hu->priv = bcm;
  82. return 0;
  83. }
  84. static int bcm_close(struct hci_uart *hu)
  85. {
  86. struct bcm_data *bcm = hu->priv;
  87. BT_DBG("hu %p", hu);
  88. skb_queue_purge(&bcm->txq);
  89. kfree_skb(bcm->rx_skb);
  90. kfree(bcm);
  91. hu->priv = NULL;
  92. return 0;
  93. }
  94. static int bcm_flush(struct hci_uart *hu)
  95. {
  96. struct bcm_data *bcm = hu->priv;
  97. BT_DBG("hu %p", hu);
  98. skb_queue_purge(&bcm->txq);
  99. return 0;
  100. }
  101. static int bcm_setup(struct hci_uart *hu)
  102. {
  103. char fw_name[64];
  104. const struct firmware *fw;
  105. unsigned int speed;
  106. int err;
  107. BT_DBG("hu %p", hu);
  108. hu->hdev->set_bdaddr = btbcm_set_bdaddr;
  109. err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
  110. if (err)
  111. return err;
  112. err = reject_firmware(&fw, fw_name, &hu->hdev->dev);
  113. if (err < 0) {
  114. BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name);
  115. return 0;
  116. }
  117. err = btbcm_patchram(hu->hdev, fw);
  118. if (err) {
  119. BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err);
  120. goto finalize;
  121. }
  122. /* Init speed if any */
  123. if (hu->init_speed)
  124. speed = hu->init_speed;
  125. else if (hu->proto->init_speed)
  126. speed = hu->proto->init_speed;
  127. else
  128. speed = 0;
  129. if (speed)
  130. hci_uart_set_baudrate(hu, speed);
  131. /* Operational speed if any */
  132. if (hu->oper_speed)
  133. speed = hu->oper_speed;
  134. else if (hu->proto->oper_speed)
  135. speed = hu->proto->oper_speed;
  136. else
  137. speed = 0;
  138. if (speed) {
  139. err = bcm_set_baudrate(hu, speed);
  140. if (!err)
  141. hci_uart_set_baudrate(hu, speed);
  142. }
  143. finalize:
  144. release_firmware(fw);
  145. err = btbcm_finalize(hu->hdev);
  146. return err;
  147. }
  148. static const struct h4_recv_pkt bcm_recv_pkts[] = {
  149. { H4_RECV_ACL, .recv = hci_recv_frame },
  150. { H4_RECV_SCO, .recv = hci_recv_frame },
  151. { H4_RECV_EVENT, .recv = hci_recv_frame },
  152. };
  153. static int bcm_recv(struct hci_uart *hu, const void *data, int count)
  154. {
  155. struct bcm_data *bcm = hu->priv;
  156. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  157. return -EUNATCH;
  158. bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
  159. bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
  160. if (IS_ERR(bcm->rx_skb)) {
  161. int err = PTR_ERR(bcm->rx_skb);
  162. BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
  163. bcm->rx_skb = NULL;
  164. return err;
  165. }
  166. return count;
  167. }
  168. static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  169. {
  170. struct bcm_data *bcm = hu->priv;
  171. BT_DBG("hu %p skb %p", hu, skb);
  172. /* Prepend skb with frame type */
  173. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  174. skb_queue_tail(&bcm->txq, skb);
  175. return 0;
  176. }
  177. static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
  178. {
  179. struct bcm_data *bcm = hu->priv;
  180. return skb_dequeue(&bcm->txq);
  181. }
  182. static const struct hci_uart_proto bcm_proto = {
  183. .id = HCI_UART_BCM,
  184. .name = "BCM",
  185. .init_speed = 115200,
  186. .oper_speed = 4000000,
  187. .open = bcm_open,
  188. .close = bcm_close,
  189. .flush = bcm_flush,
  190. .setup = bcm_setup,
  191. .set_baudrate = bcm_set_baudrate,
  192. .recv = bcm_recv,
  193. .enqueue = bcm_enqueue,
  194. .dequeue = bcm_dequeue,
  195. };
  196. int __init bcm_init(void)
  197. {
  198. return hci_uart_register_proto(&bcm_proto);
  199. }
  200. int __exit bcm_deinit(void)
  201. {
  202. return hci_uart_unregister_proto(&bcm_proto);
  203. }