3 * Bluetooth HCI UART driver for Broadcom devices
5 * Copyright (C) 2015 Intel Corporation
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.
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.
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
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/skbuff.h>
27 #include <linux/firmware.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
36 struct sk_buff *rx_skb;
37 struct sk_buff_head txq;
40 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
42 struct hci_dev *hdev = hu->hdev;
44 struct bcm_update_uart_baud_rate param;
46 if (speed > 3000000) {
47 struct bcm_write_uart_clock_setting clock;
49 clock.type = BCM_UART_CLOCK_48MHZ;
51 BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type);
53 /* This Broadcom specific command changes the UART's controller
54 * clock for baud rate > 3000000.
56 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
58 int err = PTR_ERR(skb);
59 BT_ERR("%s: BCM: failed to write clock command (%d)",
67 BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed);
69 param.zero = cpu_to_le16(0);
70 param.baud_rate = cpu_to_le32(speed);
72 /* This Broadcom specific command changes the UART's controller baud
75 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), ¶m,
78 int err = PTR_ERR(skb);
79 BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
89 static int bcm_open(struct hci_uart *hu)
95 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
99 skb_queue_head_init(&bcm->txq);
105 static int bcm_close(struct hci_uart *hu)
107 struct bcm_data *bcm = hu->priv;
111 skb_queue_purge(&bcm->txq);
112 kfree_skb(bcm->rx_skb);
119 static int bcm_flush(struct hci_uart *hu)
121 struct bcm_data *bcm = hu->priv;
125 skb_queue_purge(&bcm->txq);
130 static int bcm_setup(struct hci_uart *hu)
133 const struct firmware *fw;
139 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
141 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
145 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
147 BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name);
151 err = btbcm_patchram(hu->hdev, fw);
153 BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err);
157 /* Init speed if any */
159 speed = hu->init_speed;
160 else if (hu->proto->init_speed)
161 speed = hu->proto->init_speed;
166 hci_uart_set_baudrate(hu, speed);
168 /* Operational speed if any */
170 speed = hu->oper_speed;
171 else if (hu->proto->oper_speed)
172 speed = hu->proto->oper_speed;
177 err = bcm_set_baudrate(hu, speed);
179 hci_uart_set_baudrate(hu, speed);
183 release_firmware(fw);
185 err = btbcm_finalize(hu->hdev);
190 static const struct h4_recv_pkt bcm_recv_pkts[] = {
191 { H4_RECV_ACL, .recv = hci_recv_frame },
192 { H4_RECV_SCO, .recv = hci_recv_frame },
193 { H4_RECV_EVENT, .recv = hci_recv_frame },
196 static int bcm_recv(struct hci_uart *hu, const void *data, int count)
198 struct bcm_data *bcm = hu->priv;
200 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
203 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
204 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
205 if (IS_ERR(bcm->rx_skb)) {
206 int err = PTR_ERR(bcm->rx_skb);
207 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
215 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
217 struct bcm_data *bcm = hu->priv;
219 BT_DBG("hu %p skb %p", hu, skb);
221 /* Prepend skb with frame type */
222 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
223 skb_queue_tail(&bcm->txq, skb);
228 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
230 struct bcm_data *bcm = hu->priv;
232 return skb_dequeue(&bcm->txq);
235 static const struct hci_uart_proto bcm_proto = {
238 .init_speed = 115200,
239 .oper_speed = 4000000,
244 .set_baudrate = bcm_set_baudrate,
246 .enqueue = bcm_enqueue,
247 .dequeue = bcm_dequeue,
250 int __init bcm_init(void)
252 return hci_uart_register_proto(&bcm_proto);
255 int __exit bcm_deinit(void)
257 return hci_uart_unregister_proto(&bcm_proto);