Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
b3190df6 SS |
2 | /* |
3 | * Atheros Communication Bluetooth HCIATH3K UART protocol | |
4 | * | |
5 | * HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's | |
6 | * power management protocol extension to H4 to support AR300x Bluetooth Chip. | |
7 | * | |
8 | * Copyright (c) 2009-2010 Atheros Communications Inc. | |
9 | * | |
10 | * Acknowledgements: | |
11 | * This file is based on hci_h4.c, which was written | |
12 | * by Maxim Krasnyansky and Marcel Holtmann. | |
b3190df6 SS |
13 | */ |
14 | ||
15 | #include <linux/module.h> | |
16 | #include <linux/kernel.h> | |
17 | ||
18 | #include <linux/init.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/tty.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/ioctl.h> | |
23 | #include <linux/skbuff.h> | |
24 | ||
25 | #include <net/bluetooth/bluetooth.h> | |
26 | #include <net/bluetooth/hci_core.h> | |
27 | ||
28 | #include "hci_uart.h" | |
29 | ||
30 | struct ath_struct { | |
31 | struct hci_uart *hu; | |
32 | unsigned int cur_sleep; | |
33 | ||
d90aa682 | 34 | struct sk_buff *rx_skb; |
b3190df6 SS |
35 | struct sk_buff_head txq; |
36 | struct work_struct ctxtsw; | |
37 | }; | |
38 | ||
13df5000 LP |
39 | #define OP_WRITE_TAG 0x01 |
40 | ||
41 | #define INDEX_BDADDR 0x01 | |
42 | ||
43 | struct ath_vendor_cmd { | |
44 | __u8 opcode; | |
45 | __le16 index; | |
46 | __u8 len; | |
47 | __u8 data[251]; | |
48 | } __packed; | |
49 | ||
b3190df6 SS |
50 | static int ath_wakeup_ar3k(struct tty_struct *tty) |
51 | { | |
afaae084 | 52 | int status = tty->driver->ops->tiocmget(tty); |
b3190df6 SS |
53 | |
54 | if (status & TIOCM_CTS) | |
55 | return status; | |
56 | ||
b3190df6 | 57 | /* Clear RTS first */ |
632f32e2 | 58 | tty->driver->ops->tiocmget(tty); |
afaae084 | 59 | tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS); |
688d6240 | 60 | msleep(20); |
b3190df6 SS |
61 | |
62 | /* Set RTS, wake up board */ | |
632f32e2 | 63 | tty->driver->ops->tiocmget(tty); |
afaae084 | 64 | tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00); |
688d6240 | 65 | msleep(20); |
b3190df6 | 66 | |
afaae084 | 67 | status = tty->driver->ops->tiocmget(tty); |
b3190df6 SS |
68 | return status; |
69 | } | |
70 | ||
71 | static void ath_hci_uart_work(struct work_struct *work) | |
72 | { | |
73 | int status; | |
74 | struct ath_struct *ath; | |
75 | struct hci_uart *hu; | |
76 | struct tty_struct *tty; | |
77 | ||
78 | ath = container_of(work, struct ath_struct, ctxtsw); | |
79 | ||
80 | hu = ath->hu; | |
81 | tty = hu->tty; | |
82 | ||
83 | /* verify and wake up controller */ | |
84 | if (ath->cur_sleep) { | |
85 | status = ath_wakeup_ar3k(tty); | |
86 | if (!(status & TIOCM_CTS)) | |
87 | return; | |
88 | } | |
89 | ||
90 | /* Ready to send Data */ | |
91 | clear_bit(HCI_UART_SENDING, &hu->tx_state); | |
92 | hci_uart_tx_wakeup(hu); | |
93 | } | |
94 | ||
b3190df6 SS |
95 | static int ath_open(struct hci_uart *hu) |
96 | { | |
97 | struct ath_struct *ath; | |
98 | ||
99 | BT_DBG("hu %p", hu); | |
100 | ||
f5fd5bae | 101 | ath = kzalloc(sizeof(*ath), GFP_KERNEL); |
b3190df6 SS |
102 | if (!ath) |
103 | return -ENOMEM; | |
104 | ||
105 | skb_queue_head_init(&ath->txq); | |
106 | ||
107 | hu->priv = ath; | |
108 | ath->hu = hu; | |
109 | ||
110 | INIT_WORK(&ath->ctxtsw, ath_hci_uart_work); | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
c0ba7acd | 115 | static int ath_close(struct hci_uart *hu) |
b3190df6 SS |
116 | { |
117 | struct ath_struct *ath = hu->priv; | |
118 | ||
119 | BT_DBG("hu %p", hu); | |
120 | ||
121 | skb_queue_purge(&ath->txq); | |
122 | ||
c0ba7acd MH |
123 | kfree_skb(ath->rx_skb); |
124 | ||
125 | cancel_work_sync(&ath->ctxtsw); | |
126 | ||
127 | hu->priv = NULL; | |
128 | kfree(ath); | |
129 | ||
b3190df6 SS |
130 | return 0; |
131 | } | |
132 | ||
c0ba7acd | 133 | static int ath_flush(struct hci_uart *hu) |
b3190df6 SS |
134 | { |
135 | struct ath_struct *ath = hu->priv; | |
136 | ||
137 | BT_DBG("hu %p", hu); | |
138 | ||
139 | skb_queue_purge(&ath->txq); | |
140 | ||
c0ba7acd MH |
141 | return 0; |
142 | } | |
d90aa682 | 143 | |
13df5000 LP |
144 | static int ath_vendor_cmd(struct hci_dev *hdev, uint8_t opcode, uint16_t index, |
145 | const void *data, size_t dlen) | |
4c876c0e MH |
146 | { |
147 | struct sk_buff *skb; | |
13df5000 LP |
148 | struct ath_vendor_cmd cmd; |
149 | ||
150 | if (dlen > sizeof(cmd.data)) | |
151 | return -EINVAL; | |
152 | ||
153 | cmd.opcode = opcode; | |
154 | cmd.index = cpu_to_le16(index); | |
155 | cmd.len = dlen; | |
156 | memcpy(cmd.data, data, dlen); | |
157 | ||
158 | skb = __hci_cmd_sync(hdev, 0xfc0b, dlen + 4, &cmd, HCI_INIT_TIMEOUT); | |
159 | if (IS_ERR(skb)) | |
160 | return PTR_ERR(skb); | |
4c876c0e MH |
161 | kfree_skb(skb); |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
13df5000 LP |
166 | static int ath_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) |
167 | { | |
168 | return ath_vendor_cmd(hdev, OP_WRITE_TAG, INDEX_BDADDR, bdaddr, | |
169 | sizeof(*bdaddr)); | |
170 | } | |
171 | ||
4c876c0e MH |
172 | static int ath_setup(struct hci_uart *hu) |
173 | { | |
174 | BT_DBG("hu %p", hu); | |
175 | ||
176 | hu->hdev->set_bdaddr = ath_set_bdaddr; | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
c0ba7acd MH |
181 | static const struct h4_recv_pkt ath_recv_pkts[] = { |
182 | { H4_RECV_ACL, .recv = hci_recv_frame }, | |
183 | { H4_RECV_SCO, .recv = hci_recv_frame }, | |
184 | { H4_RECV_EVENT, .recv = hci_recv_frame }, | |
185 | }; | |
b3190df6 | 186 | |
c0ba7acd MH |
187 | static int ath_recv(struct hci_uart *hu, const void *data, int count) |
188 | { | |
189 | struct ath_struct *ath = hu->priv; | |
b3190df6 | 190 | |
c0ba7acd MH |
191 | ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count, |
192 | ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts)); | |
193 | if (IS_ERR(ath->rx_skb)) { | |
194 | int err = PTR_ERR(ath->rx_skb); | |
2064ee33 | 195 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
37134167 | 196 | ath->rx_skb = NULL; |
c0ba7acd MH |
197 | return err; |
198 | } | |
199 | ||
200 | return count; | |
b3190df6 SS |
201 | } |
202 | ||
203 | #define HCI_OP_ATH_SLEEP 0xFC04 | |
204 | ||
b3190df6 SS |
205 | static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
206 | { | |
207 | struct ath_struct *ath = hu->priv; | |
208 | ||
618e8bc2 | 209 | if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT) { |
4ebaa4ed | 210 | kfree_skb(skb); |
b3190df6 SS |
211 | return 0; |
212 | } | |
213 | ||
c0ba7acd | 214 | /* Update power management enable flag with parameters of |
b3190df6 SS |
215 | * HCI sleep enable vendor specific HCI command. |
216 | */ | |
618e8bc2 | 217 | if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) { |
b3190df6 SS |
218 | struct hci_command_hdr *hdr = (void *)skb->data; |
219 | ||
220 | if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP) | |
221 | ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE]; | |
222 | } | |
223 | ||
224 | BT_DBG("hu %p skb %p", hu, skb); | |
225 | ||
226 | /* Prepend skb with frame type */ | |
618e8bc2 | 227 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
b3190df6 SS |
228 | |
229 | skb_queue_tail(&ath->txq, skb); | |
230 | set_bit(HCI_UART_SENDING, &hu->tx_state); | |
231 | ||
232 | schedule_work(&ath->ctxtsw); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | static struct sk_buff *ath_dequeue(struct hci_uart *hu) | |
238 | { | |
239 | struct ath_struct *ath = hu->priv; | |
240 | ||
241 | return skb_dequeue(&ath->txq); | |
242 | } | |
243 | ||
4ee7ef19 | 244 | static const struct hci_uart_proto athp = { |
7c40fb8d MH |
245 | .id = HCI_UART_ATH3K, |
246 | .name = "ATH3K", | |
aee61f7a | 247 | .manufacturer = 69, |
7c40fb8d MH |
248 | .open = ath_open, |
249 | .close = ath_close, | |
c0ba7acd | 250 | .flush = ath_flush, |
4c876c0e | 251 | .setup = ath_setup, |
7c40fb8d MH |
252 | .recv = ath_recv, |
253 | .enqueue = ath_enqueue, | |
254 | .dequeue = ath_dequeue, | |
b3190df6 SS |
255 | }; |
256 | ||
f2b94bb9 | 257 | int __init ath_init(void) |
b3190df6 | 258 | { |
01009eec | 259 | return hci_uart_register_proto(&athp); |
b3190df6 SS |
260 | } |
261 | ||
f2b94bb9 | 262 | int __exit ath_deinit(void) |
b3190df6 SS |
263 | { |
264 | return hci_uart_unregister_proto(&athp); | |
265 | } |