blob: e4d66b61cda0e0714d0e927c99b1b4f59c1febb2 [file] [log] [blame]
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -07001/*
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
24#include <linux/kernel.h>
25#include <linux/errno.h>
26#include <linux/skbuff.h>
Frederic Danis18aeb442015-05-28 11:25:01 +020027#include <linux/firmware.h>
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -070028
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31
Marcel Holtmannbdd88182015-04-05 22:52:18 -070032#include "btbcm.h"
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -070033#include "hci_uart.h"
Marcel Holtmannbdd88182015-04-05 22:52:18 -070034
35struct bcm_data {
36 struct sk_buff *rx_skb;
37 struct sk_buff_head txq;
38};
39
Frederic Danis61b2fc22015-06-09 16:15:37 +020040static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
41{
42 struct hci_dev *hdev = hu->hdev;
43 struct sk_buff *skb;
44 struct bcm_update_uart_baud_rate param;
45
46 if (speed > 3000000) {
47 struct bcm_write_uart_clock_setting clock;
48
49 clock.type = BCM_UART_CLOCK_48MHZ;
50
51 BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type);
52
53 /* This Broadcom specific command changes the UART's controller
54 * clock for baud rate > 3000000.
55 */
56 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
57 if (IS_ERR(skb)) {
58 int err = PTR_ERR(skb);
59 BT_ERR("%s: BCM: failed to write clock command (%d)",
60 hdev->name, err);
61 return err;
62 }
63
64 kfree_skb(skb);
65 }
66
67 BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed);
68
69 param.zero = cpu_to_le16(0);
70 param.baud_rate = cpu_to_le32(speed);
71
72 /* This Broadcom specific command changes the UART's controller baud
73 * rate.
74 */
75 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
76 HCI_INIT_TIMEOUT);
77 if (IS_ERR(skb)) {
78 int err = PTR_ERR(skb);
79 BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
80 hdev->name, err);
81 return err;
82 }
83
84 kfree_skb(skb);
85
86 return 0;
87}
88
Marcel Holtmannbdd88182015-04-05 22:52:18 -070089static int bcm_open(struct hci_uart *hu)
90{
91 struct bcm_data *bcm;
92
93 BT_DBG("hu %p", hu);
94
95 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
96 if (!bcm)
97 return -ENOMEM;
98
99 skb_queue_head_init(&bcm->txq);
100
101 hu->priv = bcm;
102 return 0;
103}
104
105static int bcm_close(struct hci_uart *hu)
106{
107 struct bcm_data *bcm = hu->priv;
108
109 BT_DBG("hu %p", hu);
110
111 skb_queue_purge(&bcm->txq);
112 kfree_skb(bcm->rx_skb);
113 kfree(bcm);
114
115 hu->priv = NULL;
116 return 0;
117}
118
119static int bcm_flush(struct hci_uart *hu)
120{
121 struct bcm_data *bcm = hu->priv;
122
123 BT_DBG("hu %p", hu);
124
125 skb_queue_purge(&bcm->txq);
126
127 return 0;
128}
129
130static int bcm_setup(struct hci_uart *hu)
131{
Frederic Danis6be09b42015-05-28 11:25:05 +0200132 char fw_name[64];
133 const struct firmware *fw;
134 int err;
135
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700136 BT_DBG("hu %p", hu);
137
138 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
139
Frederic Danis6be09b42015-05-28 11:25:05 +0200140 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
141 if (err)
142 return err;
143
144 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
145 if (err < 0) {
146 BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name);
147 return 0;
148 }
149
150 err = btbcm_patchram(hu->hdev, fw);
151 if (err) {
152 BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err);
153 goto finalize;
154 }
155
156 if (hu->proto->init_speed)
157 hci_uart_set_baudrate(hu, hu->proto->init_speed);
158
Frederic Danis61b2fc22015-06-09 16:15:37 +0200159 if (hu->proto->oper_speed) {
160 err = bcm_set_baudrate(hu, hu->proto->oper_speed);
161 if (!err)
162 hci_uart_set_baudrate(hu, hu->proto->oper_speed);
163 }
164
Frederic Danis6be09b42015-05-28 11:25:05 +0200165finalize:
166 release_firmware(fw);
167
168 err = btbcm_finalize(hu->hdev);
169
170 return err;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700171}
172
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700173static const struct h4_recv_pkt bcm_recv_pkts[] = {
174 { H4_RECV_ACL, .recv = hci_recv_frame },
175 { H4_RECV_SCO, .recv = hci_recv_frame },
176 { H4_RECV_EVENT, .recv = hci_recv_frame },
177};
178
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700179static int bcm_recv(struct hci_uart *hu, const void *data, int count)
180{
181 struct bcm_data *bcm = hu->priv;
182
183 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
184 return -EUNATCH;
185
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700186 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
187 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700188 if (IS_ERR(bcm->rx_skb)) {
189 int err = PTR_ERR(bcm->rx_skb);
190 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
191 return err;
192 }
193
194 return count;
195}
196
197static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
198{
199 struct bcm_data *bcm = hu->priv;
200
201 BT_DBG("hu %p skb %p", hu, skb);
202
203 /* Prepend skb with frame type */
204 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
205 skb_queue_tail(&bcm->txq, skb);
206
207 return 0;
208}
209
210static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
211{
212 struct bcm_data *bcm = hu->priv;
213
214 return skb_dequeue(&bcm->txq);
215}
216
217static const struct hci_uart_proto bcm_proto = {
218 .id = HCI_UART_BCM,
219 .name = "BCM",
Frederic Danis61b2fc22015-06-09 16:15:37 +0200220 .init_speed = 115200,
221 .oper_speed = 4000000,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700222 .open = bcm_open,
223 .close = bcm_close,
224 .flush = bcm_flush,
225 .setup = bcm_setup,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200226 .set_baudrate = bcm_set_baudrate,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700227 .recv = bcm_recv,
228 .enqueue = bcm_enqueue,
229 .dequeue = bcm_dequeue,
230};
231
232int __init bcm_init(void)
233{
234 return hci_uart_register_proto(&bcm_proto);
235}
236
237int __exit bcm_deinit(void)
238{
239 return hci_uart_unregister_proto(&bcm_proto);
240}