]>
Commit | Line | Data |
---|---|---|
e9a2dd26 MH |
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 | ||
24 | #include <linux/kernel.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/skbuff.h> | |
18aeb444 | 27 | #include <linux/firmware.h> |
e9a2dd26 MH |
28 | |
29 | #include <net/bluetooth/bluetooth.h> | |
30 | #include <net/bluetooth/hci_core.h> | |
31 | ||
bdd8818e | 32 | #include "btbcm.h" |
e9a2dd26 | 33 | #include "hci_uart.h" |
bdd8818e MH |
34 | |
35 | struct bcm_data { | |
36 | struct sk_buff *rx_skb; | |
37 | struct sk_buff_head txq; | |
38 | }; | |
39 | ||
40 | static int bcm_open(struct hci_uart *hu) | |
41 | { | |
42 | struct bcm_data *bcm; | |
43 | ||
44 | BT_DBG("hu %p", hu); | |
45 | ||
46 | bcm = kzalloc(sizeof(*bcm), GFP_KERNEL); | |
47 | if (!bcm) | |
48 | return -ENOMEM; | |
49 | ||
50 | skb_queue_head_init(&bcm->txq); | |
51 | ||
52 | hu->priv = bcm; | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static int bcm_close(struct hci_uart *hu) | |
57 | { | |
58 | struct bcm_data *bcm = hu->priv; | |
59 | ||
60 | BT_DBG("hu %p", hu); | |
61 | ||
62 | skb_queue_purge(&bcm->txq); | |
63 | kfree_skb(bcm->rx_skb); | |
64 | kfree(bcm); | |
65 | ||
66 | hu->priv = NULL; | |
67 | return 0; | |
68 | } | |
69 | ||
70 | static int bcm_flush(struct hci_uart *hu) | |
71 | { | |
72 | struct bcm_data *bcm = hu->priv; | |
73 | ||
74 | BT_DBG("hu %p", hu); | |
75 | ||
76 | skb_queue_purge(&bcm->txq); | |
77 | ||
78 | return 0; | |
79 | } | |
80 | ||
81 | static int bcm_setup(struct hci_uart *hu) | |
82 | { | |
6be09b48 FD |
83 | char fw_name[64]; |
84 | const struct firmware *fw; | |
85 | int err; | |
86 | ||
bdd8818e MH |
87 | BT_DBG("hu %p", hu); |
88 | ||
89 | hu->hdev->set_bdaddr = btbcm_set_bdaddr; | |
90 | ||
6be09b48 FD |
91 | err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name)); |
92 | if (err) | |
93 | return err; | |
94 | ||
95 | err = request_firmware(&fw, fw_name, &hu->hdev->dev); | |
96 | if (err < 0) { | |
97 | BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name); | |
98 | return 0; | |
99 | } | |
100 | ||
101 | err = btbcm_patchram(hu->hdev, fw); | |
102 | if (err) { | |
103 | BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err); | |
104 | goto finalize; | |
105 | } | |
106 | ||
107 | if (hu->proto->init_speed) | |
108 | hci_uart_set_baudrate(hu, hu->proto->init_speed); | |
109 | ||
110 | finalize: | |
111 | release_firmware(fw); | |
112 | ||
113 | err = btbcm_finalize(hu->hdev); | |
114 | ||
115 | return err; | |
bdd8818e MH |
116 | } |
117 | ||
79b8df93 MH |
118 | static const struct h4_recv_pkt bcm_recv_pkts[] = { |
119 | { H4_RECV_ACL, .recv = hci_recv_frame }, | |
120 | { H4_RECV_SCO, .recv = hci_recv_frame }, | |
121 | { H4_RECV_EVENT, .recv = hci_recv_frame }, | |
122 | }; | |
123 | ||
bdd8818e MH |
124 | static int bcm_recv(struct hci_uart *hu, const void *data, int count) |
125 | { | |
126 | struct bcm_data *bcm = hu->priv; | |
127 | ||
128 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) | |
129 | return -EUNATCH; | |
130 | ||
79b8df93 MH |
131 | bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count, |
132 | bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts)); | |
bdd8818e MH |
133 | if (IS_ERR(bcm->rx_skb)) { |
134 | int err = PTR_ERR(bcm->rx_skb); | |
135 | BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err); | |
136 | return err; | |
137 | } | |
138 | ||
139 | return count; | |
140 | } | |
141 | ||
142 | static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
143 | { | |
144 | struct bcm_data *bcm = hu->priv; | |
145 | ||
146 | BT_DBG("hu %p skb %p", hu, skb); | |
147 | ||
148 | /* Prepend skb with frame type */ | |
149 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); | |
150 | skb_queue_tail(&bcm->txq, skb); | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
155 | static struct sk_buff *bcm_dequeue(struct hci_uart *hu) | |
156 | { | |
157 | struct bcm_data *bcm = hu->priv; | |
158 | ||
159 | return skb_dequeue(&bcm->txq); | |
160 | } | |
161 | ||
162 | static const struct hci_uart_proto bcm_proto = { | |
163 | .id = HCI_UART_BCM, | |
164 | .name = "BCM", | |
165 | .open = bcm_open, | |
166 | .close = bcm_close, | |
167 | .flush = bcm_flush, | |
168 | .setup = bcm_setup, | |
169 | .recv = bcm_recv, | |
170 | .enqueue = bcm_enqueue, | |
171 | .dequeue = bcm_dequeue, | |
172 | }; | |
173 | ||
174 | int __init bcm_init(void) | |
175 | { | |
176 | return hci_uart_register_proto(&bcm_proto); | |
177 | } | |
178 | ||
179 | int __exit bcm_deinit(void) | |
180 | { | |
181 | return hci_uart_unregister_proto(&bcm_proto); | |
182 | } |