Commit | Line | Data |
---|---|---|
7dec65c8 JH |
1 | /* |
2 | * | |
3 | * Bluetooth HCI Three-wire UART driver | |
4 | * | |
5 | * Copyright (C) 2012 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 | ||
4eb3cbc4 | 24 | #include <linux/acpi.h> |
7dec65c8 | 25 | #include <linux/errno.h> |
4c791489 | 26 | #include <linux/gpio/consumer.h> |
4eb3cbc4 JC |
27 | #include <linux/kernel.h> |
28 | #include <linux/mod_devicetable.h> | |
ce945552 | 29 | #include <linux/serdev.h> |
7dec65c8 JH |
30 | #include <linux/skbuff.h> |
31 | ||
32 | #include <net/bluetooth/bluetooth.h> | |
33 | #include <net/bluetooth/hci_core.h> | |
34 | ||
b825d7c4 | 35 | #include "btrtl.h" |
7dec65c8 JH |
36 | #include "hci_uart.h" |
37 | ||
c0a1b73c JH |
38 | #define HCI_3WIRE_ACK_PKT 0 |
39 | #define HCI_3WIRE_LINK_PKT 15 | |
40 | ||
afdc944c JH |
41 | /* Sliding window size */ |
42 | #define H5_TX_WIN_MAX 4 | |
3f27e95b JH |
43 | |
44 | #define H5_ACK_TIMEOUT msecs_to_jiffies(250) | |
40f10224 | 45 | #define H5_SYNC_TIMEOUT msecs_to_jiffies(100) |
3f27e95b | 46 | |
bc1f35b9 JH |
47 | /* |
48 | * Maximum Three-wire packet: | |
49 | * 4 byte header + max value for 12-bit length + 2 bytes for CRC | |
50 | */ | |
51 | #define H5_MAX_LEN (4 + 0xfff + 2) | |
52 | ||
01977c0b JH |
53 | /* Convenience macros for reading Three-wire header values */ |
54 | #define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07) | |
55 | #define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07) | |
56 | #define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01) | |
57 | #define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01) | |
58 | #define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f) | |
4223f368 | 59 | #define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0x0f) + ((hdr)[2] << 4)) |
01977c0b | 60 | |
bc1f35b9 JH |
61 | #define SLIP_DELIMITER 0xc0 |
62 | #define SLIP_ESC 0xdb | |
63 | #define SLIP_ESC_DELIM 0xdc | |
64 | #define SLIP_ESC_ESC 0xdd | |
65 | ||
e0482103 JH |
66 | /* H5 state flags */ |
67 | enum { | |
68 | H5_RX_ESC, /* SLIP escape mode */ | |
69 | H5_TX_ACK_REQ, /* Pending ack to send */ | |
70 | }; | |
71 | ||
7d664fba | 72 | struct h5 { |
ce945552 HG |
73 | /* Must be the first member, hci_serdev.c expects this. */ |
74 | struct hci_uart serdev_hu; | |
75 | ||
bc1f35b9 JH |
76 | struct sk_buff_head unack; /* Unack'ed packets queue */ |
77 | struct sk_buff_head rel; /* Reliable packets queue */ | |
78 | struct sk_buff_head unrel; /* Unreliable packets queue */ | |
79 | ||
e0482103 JH |
80 | unsigned long flags; |
81 | ||
bc1f35b9 JH |
82 | struct sk_buff *rx_skb; /* Receive buffer */ |
83 | size_t rx_pending; /* Expecting more bytes */ | |
43eb12d7 | 84 | u8 rx_ack; /* Last ack number received */ |
7d664fba | 85 | |
fa4cf04e | 86 | int (*rx_func)(struct hci_uart *hu, u8 c); |
7d664fba | 87 | |
bc1f35b9 | 88 | struct timer_list timer; /* Retransmission timer */ |
04356052 | 89 | struct hci_uart *hu; /* Parent HCI UART */ |
3f27e95b | 90 | |
43eb12d7 | 91 | u8 tx_seq; /* Next seq number to send */ |
40f10224 | 92 | u8 tx_ack; /* Next ack number to send */ |
afdc944c | 93 | u8 tx_win; /* Sliding window size */ |
10122d07 | 94 | |
f674a057 JH |
95 | enum { |
96 | H5_UNINITIALIZED, | |
97 | H5_INITIALIZED, | |
98 | H5_ACTIVE, | |
99 | } state; | |
100 | ||
95c5c220 JH |
101 | enum { |
102 | H5_AWAKE, | |
103 | H5_SLEEPING, | |
104 | H5_WAKING_UP, | |
105 | } sleep; | |
4eb3cbc4 JC |
106 | |
107 | const struct h5_vnd *vnd; | |
108 | const char *id; | |
4c791489 HG |
109 | |
110 | struct gpio_desc *enable_gpio; | |
111 | struct gpio_desc *device_wake_gpio; | |
4eb3cbc4 JC |
112 | }; |
113 | ||
114 | struct h5_vnd { | |
115 | int (*setup)(struct h5 *h5); | |
116 | void (*open)(struct h5 *h5); | |
117 | void (*close)(struct h5 *h5); | |
4c791489 | 118 | const struct acpi_gpio_mapping *acpi_gpio_map; |
7d664fba JH |
119 | }; |
120 | ||
bc1f35b9 JH |
121 | static void h5_reset_rx(struct h5 *h5); |
122 | ||
f674a057 JH |
123 | static void h5_link_control(struct hci_uart *hu, const void *data, size_t len) |
124 | { | |
125 | struct h5 *h5 = hu->priv; | |
126 | struct sk_buff *nskb; | |
127 | ||
128 | nskb = alloc_skb(3, GFP_ATOMIC); | |
129 | if (!nskb) | |
130 | return; | |
131 | ||
618e8bc2 | 132 | hci_skb_pkt_type(nskb) = HCI_3WIRE_LINK_PKT; |
f674a057 | 133 | |
59ae1d12 | 134 | skb_put_data(nskb, data, len); |
f674a057 JH |
135 | |
136 | skb_queue_tail(&h5->unrel, nskb); | |
137 | } | |
138 | ||
afdc944c JH |
139 | static u8 h5_cfg_field(struct h5 *h5) |
140 | { | |
afdc944c | 141 | /* Sliding window size (first 3 bits) */ |
742c5951 | 142 | return h5->tx_win & 0x07; |
afdc944c JH |
143 | } |
144 | ||
04356052 | 145 | static void h5_timed_event(struct timer_list *t) |
3f27e95b | 146 | { |
f674a057 | 147 | const unsigned char sync_req[] = { 0x01, 0x7e }; |
87a6b9bd | 148 | unsigned char conf_req[3] = { 0x03, 0xfc }; |
04356052 KC |
149 | struct h5 *h5 = from_timer(h5, t, timer); |
150 | struct hci_uart *hu = h5->hu; | |
3f27e95b JH |
151 | struct sk_buff *skb; |
152 | unsigned long flags; | |
153 | ||
95c5c220 JH |
154 | BT_DBG("%s", hu->hdev->name); |
155 | ||
f674a057 JH |
156 | if (h5->state == H5_UNINITIALIZED) |
157 | h5_link_control(hu, sync_req, sizeof(sync_req)); | |
158 | ||
afdc944c JH |
159 | if (h5->state == H5_INITIALIZED) { |
160 | conf_req[2] = h5_cfg_field(h5); | |
f674a057 | 161 | h5_link_control(hu, conf_req, sizeof(conf_req)); |
afdc944c | 162 | } |
f674a057 JH |
163 | |
164 | if (h5->state != H5_ACTIVE) { | |
165 | mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT); | |
166 | goto wakeup; | |
167 | } | |
168 | ||
95c5c220 JH |
169 | if (h5->sleep != H5_AWAKE) { |
170 | h5->sleep = H5_SLEEPING; | |
171 | goto wakeup; | |
172 | } | |
173 | ||
3f27e95b JH |
174 | BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen); |
175 | ||
176 | spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING); | |
177 | ||
178 | while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) { | |
43eb12d7 | 179 | h5->tx_seq = (h5->tx_seq - 1) & 0x07; |
3f27e95b JH |
180 | skb_queue_head(&h5->rel, skb); |
181 | } | |
182 | ||
183 | spin_unlock_irqrestore(&h5->unack.lock, flags); | |
184 | ||
f674a057 | 185 | wakeup: |
3f27e95b JH |
186 | hci_uart_tx_wakeup(hu); |
187 | } | |
188 | ||
b509c02d LP |
189 | static void h5_peer_reset(struct hci_uart *hu) |
190 | { | |
191 | struct h5 *h5 = hu->priv; | |
b509c02d LP |
192 | |
193 | BT_ERR("Peer device has reset"); | |
194 | ||
195 | h5->state = H5_UNINITIALIZED; | |
196 | ||
197 | del_timer(&h5->timer); | |
198 | ||
199 | skb_queue_purge(&h5->rel); | |
200 | skb_queue_purge(&h5->unrel); | |
201 | skb_queue_purge(&h5->unack); | |
202 | ||
203 | h5->tx_seq = 0; | |
204 | h5->tx_ack = 0; | |
205 | ||
882809fb MH |
206 | /* Send reset request to upper stack */ |
207 | hci_reset_dev(hu->hdev); | |
b509c02d LP |
208 | } |
209 | ||
7dec65c8 JH |
210 | static int h5_open(struct hci_uart *hu) |
211 | { | |
7d664fba | 212 | struct h5 *h5; |
40f10224 | 213 | const unsigned char sync[] = { 0x01, 0x7e }; |
7d664fba JH |
214 | |
215 | BT_DBG("hu %p", hu); | |
216 | ||
ce945552 HG |
217 | if (hu->serdev) { |
218 | h5 = serdev_device_get_drvdata(hu->serdev); | |
219 | } else { | |
220 | h5 = kzalloc(sizeof(*h5), GFP_KERNEL); | |
221 | if (!h5) | |
222 | return -ENOMEM; | |
223 | } | |
7d664fba JH |
224 | |
225 | hu->priv = h5; | |
04356052 | 226 | h5->hu = hu; |
7d664fba JH |
227 | |
228 | skb_queue_head_init(&h5->unack); | |
229 | skb_queue_head_init(&h5->rel); | |
230 | skb_queue_head_init(&h5->unrel); | |
231 | ||
bc1f35b9 JH |
232 | h5_reset_rx(h5); |
233 | ||
04356052 | 234 | timer_setup(&h5->timer, h5_timed_event, 0); |
3f27e95b | 235 | |
afdc944c JH |
236 | h5->tx_win = H5_TX_WIN_MAX; |
237 | ||
4eb3cbc4 JC |
238 | if (h5->vnd && h5->vnd->open) |
239 | h5->vnd->open(h5); | |
240 | ||
cd1b4425 JH |
241 | set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags); |
242 | ||
40f10224 JH |
243 | /* Send initial sync request */ |
244 | h5_link_control(hu, sync, sizeof(sync)); | |
245 | mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT); | |
246 | ||
7d664fba | 247 | return 0; |
7dec65c8 JH |
248 | } |
249 | ||
250 | static int h5_close(struct hci_uart *hu) | |
251 | { | |
7d664fba JH |
252 | struct h5 *h5 = hu->priv; |
253 | ||
c327cddd MK |
254 | del_timer_sync(&h5->timer); |
255 | ||
7d664fba JH |
256 | skb_queue_purge(&h5->unack); |
257 | skb_queue_purge(&h5->rel); | |
258 | skb_queue_purge(&h5->unrel); | |
259 | ||
4eb3cbc4 JC |
260 | if (h5->vnd && h5->vnd->close) |
261 | h5->vnd->close(h5); | |
262 | ||
ce945552 HG |
263 | if (!hu->serdev) |
264 | kfree(h5); | |
7d664fba JH |
265 | |
266 | return 0; | |
7dec65c8 JH |
267 | } |
268 | ||
4eb3cbc4 JC |
269 | static int h5_setup(struct hci_uart *hu) |
270 | { | |
271 | struct h5 *h5 = hu->priv; | |
272 | ||
273 | if (h5->vnd && h5->vnd->setup) | |
274 | return h5->vnd->setup(h5); | |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
43eb12d7 JH |
279 | static void h5_pkt_cull(struct h5 *h5) |
280 | { | |
281 | struct sk_buff *skb, *tmp; | |
282 | unsigned long flags; | |
283 | int i, to_remove; | |
284 | u8 seq; | |
285 | ||
286 | spin_lock_irqsave(&h5->unack.lock, flags); | |
287 | ||
288 | to_remove = skb_queue_len(&h5->unack); | |
40f10224 JH |
289 | if (to_remove == 0) |
290 | goto unlock; | |
43eb12d7 JH |
291 | |
292 | seq = h5->tx_seq; | |
293 | ||
294 | while (to_remove > 0) { | |
295 | if (h5->rx_ack == seq) | |
296 | break; | |
297 | ||
298 | to_remove--; | |
4807b518 | 299 | seq = (seq - 1) & 0x07; |
43eb12d7 JH |
300 | } |
301 | ||
302 | if (seq != h5->rx_ack) | |
303 | BT_ERR("Controller acked invalid packet"); | |
304 | ||
305 | i = 0; | |
306 | skb_queue_walk_safe(&h5->unack, skb, tmp) { | |
307 | if (i++ >= to_remove) | |
308 | break; | |
309 | ||
310 | __skb_unlink(skb, &h5->unack); | |
311 | kfree_skb(skb); | |
312 | } | |
313 | ||
314 | if (skb_queue_empty(&h5->unack)) | |
315 | del_timer(&h5->timer); | |
316 | ||
40f10224 | 317 | unlock: |
43eb12d7 JH |
318 | spin_unlock_irqrestore(&h5->unack.lock, flags); |
319 | } | |
320 | ||
bc1f35b9 JH |
321 | static void h5_handle_internal_rx(struct hci_uart *hu) |
322 | { | |
40f10224 JH |
323 | struct h5 *h5 = hu->priv; |
324 | const unsigned char sync_req[] = { 0x01, 0x7e }; | |
325 | const unsigned char sync_rsp[] = { 0x02, 0x7d }; | |
87a6b9bd | 326 | unsigned char conf_req[3] = { 0x03, 0xfc }; |
afdc944c | 327 | const unsigned char conf_rsp[] = { 0x04, 0x7b }; |
10122d07 JH |
328 | const unsigned char wakeup_req[] = { 0x05, 0xfa }; |
329 | const unsigned char woken_req[] = { 0x06, 0xf9 }; | |
330 | const unsigned char sleep_req[] = { 0x07, 0x78 }; | |
40f10224 JH |
331 | const unsigned char *hdr = h5->rx_skb->data; |
332 | const unsigned char *data = &h5->rx_skb->data[4]; | |
333 | ||
bc1f35b9 | 334 | BT_DBG("%s", hu->hdev->name); |
40f10224 JH |
335 | |
336 | if (H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) | |
337 | return; | |
338 | ||
339 | if (H5_HDR_LEN(hdr) < 2) | |
340 | return; | |
341 | ||
afdc944c JH |
342 | conf_req[2] = h5_cfg_field(h5); |
343 | ||
40f10224 | 344 | if (memcmp(data, sync_req, 2) == 0) { |
b509c02d LP |
345 | if (h5->state == H5_ACTIVE) |
346 | h5_peer_reset(hu); | |
40f10224 JH |
347 | h5_link_control(hu, sync_rsp, 2); |
348 | } else if (memcmp(data, sync_rsp, 2) == 0) { | |
b509c02d LP |
349 | if (h5->state == H5_ACTIVE) |
350 | h5_peer_reset(hu); | |
f674a057 | 351 | h5->state = H5_INITIALIZED; |
40f10224 JH |
352 | h5_link_control(hu, conf_req, 3); |
353 | } else if (memcmp(data, conf_req, 2) == 0) { | |
354 | h5_link_control(hu, conf_rsp, 2); | |
355 | h5_link_control(hu, conf_req, 3); | |
356 | } else if (memcmp(data, conf_rsp, 2) == 0) { | |
afdc944c | 357 | if (H5_HDR_LEN(hdr) > 2) |
1d3a1e69 | 358 | h5->tx_win = (data[2] & 0x07); |
afdc944c | 359 | BT_DBG("Three-wire init complete. tx_win %u", h5->tx_win); |
f674a057 | 360 | h5->state = H5_ACTIVE; |
cd1b4425 | 361 | hci_uart_init_ready(hu); |
40f10224 | 362 | return; |
10122d07 JH |
363 | } else if (memcmp(data, sleep_req, 2) == 0) { |
364 | BT_DBG("Peer went to sleep"); | |
95c5c220 JH |
365 | h5->sleep = H5_SLEEPING; |
366 | return; | |
10122d07 JH |
367 | } else if (memcmp(data, woken_req, 2) == 0) { |
368 | BT_DBG("Peer woke up"); | |
95c5c220 JH |
369 | h5->sleep = H5_AWAKE; |
370 | } else if (memcmp(data, wakeup_req, 2) == 0) { | |
371 | BT_DBG("Peer requested wakeup"); | |
372 | h5_link_control(hu, woken_req, 2); | |
373 | h5->sleep = H5_AWAKE; | |
40f10224 JH |
374 | } else { |
375 | BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]); | |
376 | return; | |
377 | } | |
378 | ||
379 | hci_uart_tx_wakeup(hu); | |
bc1f35b9 JH |
380 | } |
381 | ||
382 | static void h5_complete_rx_pkt(struct hci_uart *hu) | |
383 | { | |
384 | struct h5 *h5 = hu->priv; | |
43eb12d7 | 385 | const unsigned char *hdr = h5->rx_skb->data; |
bc1f35b9 | 386 | |
43eb12d7 | 387 | if (H5_HDR_RELIABLE(hdr)) { |
40f10224 | 388 | h5->tx_ack = (h5->tx_ack + 1) % 8; |
e0482103 | 389 | set_bit(H5_TX_ACK_REQ, &h5->flags); |
40f10224 | 390 | hci_uart_tx_wakeup(hu); |
43eb12d7 | 391 | } |
bc1f35b9 | 392 | |
43eb12d7 JH |
393 | h5->rx_ack = H5_HDR_ACK(hdr); |
394 | ||
395 | h5_pkt_cull(h5); | |
396 | ||
397 | switch (H5_HDR_PKT_TYPE(hdr)) { | |
bc1f35b9 JH |
398 | case HCI_EVENT_PKT: |
399 | case HCI_ACLDATA_PKT: | |
400 | case HCI_SCODATA_PKT: | |
618e8bc2 | 401 | hci_skb_pkt_type(h5->rx_skb) = H5_HDR_PKT_TYPE(hdr); |
bc1f35b9 JH |
402 | |
403 | /* Remove Three-wire header */ | |
404 | skb_pull(h5->rx_skb, 4); | |
405 | ||
e1a26170 | 406 | hci_recv_frame(hu->hdev, h5->rx_skb); |
bc1f35b9 JH |
407 | h5->rx_skb = NULL; |
408 | ||
409 | break; | |
410 | ||
411 | default: | |
412 | h5_handle_internal_rx(hu); | |
413 | break; | |
414 | } | |
415 | ||
416 | h5_reset_rx(h5); | |
417 | } | |
418 | ||
419 | static int h5_rx_crc(struct hci_uart *hu, unsigned char c) | |
420 | { | |
bc1f35b9 | 421 | h5_complete_rx_pkt(hu); |
bc1f35b9 JH |
422 | |
423 | return 0; | |
424 | } | |
425 | ||
426 | static int h5_rx_payload(struct hci_uart *hu, unsigned char c) | |
427 | { | |
428 | struct h5 *h5 = hu->priv; | |
429 | const unsigned char *hdr = h5->rx_skb->data; | |
430 | ||
43eb12d7 | 431 | if (H5_HDR_CRC(hdr)) { |
bc1f35b9 JH |
432 | h5->rx_func = h5_rx_crc; |
433 | h5->rx_pending = 2; | |
434 | } else { | |
435 | h5_complete_rx_pkt(hu); | |
bc1f35b9 JH |
436 | } |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
441 | static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c) | |
442 | { | |
443 | struct h5 *h5 = hu->priv; | |
444 | const unsigned char *hdr = h5->rx_skb->data; | |
445 | ||
40f10224 JH |
446 | BT_DBG("%s rx: seq %u ack %u crc %u rel %u type %u len %u", |
447 | hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), | |
448 | H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr), | |
449 | H5_HDR_LEN(hdr)); | |
450 | ||
bc1f35b9 JH |
451 | if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) { |
452 | BT_ERR("Invalid header checksum"); | |
453 | h5_reset_rx(h5); | |
454 | return 0; | |
455 | } | |
456 | ||
40f10224 | 457 | if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) { |
43eb12d7 | 458 | BT_ERR("Out-of-order packet arrived (%u != %u)", |
40f10224 | 459 | H5_HDR_SEQ(hdr), h5->tx_ack); |
43eb12d7 JH |
460 | h5_reset_rx(h5); |
461 | return 0; | |
462 | } | |
463 | ||
f674a057 JH |
464 | if (h5->state != H5_ACTIVE && |
465 | H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) { | |
466 | BT_ERR("Non-link packet received in non-active state"); | |
467 | h5_reset_rx(h5); | |
48439d50 | 468 | return 0; |
f674a057 JH |
469 | } |
470 | ||
bc1f35b9 | 471 | h5->rx_func = h5_rx_payload; |
43eb12d7 | 472 | h5->rx_pending = H5_HDR_LEN(hdr); |
bc1f35b9 JH |
473 | |
474 | return 0; | |
475 | } | |
476 | ||
477 | static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c) | |
478 | { | |
479 | struct h5 *h5 = hu->priv; | |
480 | ||
bc1f35b9 JH |
481 | if (c == SLIP_DELIMITER) |
482 | return 1; | |
483 | ||
484 | h5->rx_func = h5_rx_3wire_hdr; | |
485 | h5->rx_pending = 4; | |
486 | ||
487 | h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC); | |
488 | if (!h5->rx_skb) { | |
489 | BT_ERR("Can't allocate mem for new packet"); | |
490 | h5_reset_rx(h5); | |
491 | return -ENOMEM; | |
492 | } | |
493 | ||
4a2fa2b8 | 494 | h5->rx_skb->dev = (void *)hu->hdev; |
bc1f35b9 JH |
495 | |
496 | return 0; | |
497 | } | |
498 | ||
499 | static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c) | |
500 | { | |
501 | struct h5 *h5 = hu->priv; | |
502 | ||
bc1f35b9 JH |
503 | if (c == SLIP_DELIMITER) |
504 | h5->rx_func = h5_rx_pkt_start; | |
505 | ||
506 | return 1; | |
507 | } | |
508 | ||
509 | static void h5_unslip_one_byte(struct h5 *h5, unsigned char c) | |
510 | { | |
511 | const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC; | |
512 | const u8 *byte = &c; | |
513 | ||
e0482103 JH |
514 | if (!test_bit(H5_RX_ESC, &h5->flags) && c == SLIP_ESC) { |
515 | set_bit(H5_RX_ESC, &h5->flags); | |
bc1f35b9 JH |
516 | return; |
517 | } | |
518 | ||
e0482103 | 519 | if (test_and_clear_bit(H5_RX_ESC, &h5->flags)) { |
bc1f35b9 JH |
520 | switch (c) { |
521 | case SLIP_ESC_DELIM: | |
522 | byte = &delim; | |
523 | break; | |
524 | case SLIP_ESC_ESC: | |
525 | byte = &esc; | |
526 | break; | |
527 | default: | |
528 | BT_ERR("Invalid esc byte 0x%02hhx", c); | |
529 | h5_reset_rx(h5); | |
530 | return; | |
531 | } | |
bc1f35b9 JH |
532 | } |
533 | ||
59ae1d12 | 534 | skb_put_data(h5->rx_skb, byte, 1); |
bc1f35b9 JH |
535 | h5->rx_pending--; |
536 | ||
255a68e0 | 537 | BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending); |
bc1f35b9 JH |
538 | } |
539 | ||
540 | static void h5_reset_rx(struct h5 *h5) | |
541 | { | |
542 | if (h5->rx_skb) { | |
543 | kfree_skb(h5->rx_skb); | |
544 | h5->rx_skb = NULL; | |
545 | } | |
546 | ||
547 | h5->rx_func = h5_rx_delimiter; | |
548 | h5->rx_pending = 0; | |
e0482103 | 549 | clear_bit(H5_RX_ESC, &h5->flags); |
bc1f35b9 JH |
550 | } |
551 | ||
9d1c40eb | 552 | static int h5_recv(struct hci_uart *hu, const void *data, int count) |
7dec65c8 | 553 | { |
bc1f35b9 | 554 | struct h5 *h5 = hu->priv; |
9d1c40eb | 555 | const unsigned char *ptr = data; |
bc1f35b9 | 556 | |
255a68e0 JH |
557 | BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending, |
558 | count); | |
bc1f35b9 JH |
559 | |
560 | while (count > 0) { | |
561 | int processed; | |
562 | ||
563 | if (h5->rx_pending > 0) { | |
564 | if (*ptr == SLIP_DELIMITER) { | |
565 | BT_ERR("Too short H5 packet"); | |
566 | h5_reset_rx(h5); | |
567 | continue; | |
568 | } | |
569 | ||
570 | h5_unslip_one_byte(h5, *ptr); | |
571 | ||
572 | ptr++; count--; | |
573 | continue; | |
574 | } | |
575 | ||
576 | processed = h5->rx_func(hu, *ptr); | |
577 | if (processed < 0) | |
578 | return processed; | |
579 | ||
580 | ptr += processed; | |
581 | count -= processed; | |
582 | } | |
583 | ||
584 | return 0; | |
7dec65c8 JH |
585 | } |
586 | ||
587 | static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
588 | { | |
7d664fba JH |
589 | struct h5 *h5 = hu->priv; |
590 | ||
591 | if (skb->len > 0xfff) { | |
592 | BT_ERR("Packet too long (%u bytes)", skb->len); | |
593 | kfree_skb(skb); | |
594 | return 0; | |
595 | } | |
596 | ||
f674a057 JH |
597 | if (h5->state != H5_ACTIVE) { |
598 | BT_ERR("Ignoring HCI data in non-active state"); | |
599 | kfree_skb(skb); | |
600 | return 0; | |
601 | } | |
602 | ||
618e8bc2 | 603 | switch (hci_skb_pkt_type(skb)) { |
7d664fba JH |
604 | case HCI_ACLDATA_PKT: |
605 | case HCI_COMMAND_PKT: | |
606 | skb_queue_tail(&h5->rel, skb); | |
607 | break; | |
608 | ||
609 | case HCI_SCODATA_PKT: | |
610 | skb_queue_tail(&h5->unrel, skb); | |
611 | break; | |
612 | ||
613 | default: | |
618e8bc2 | 614 | BT_ERR("Unknown packet type %u", hci_skb_pkt_type(skb)); |
7d664fba JH |
615 | kfree_skb(skb); |
616 | break; | |
617 | } | |
618 | ||
619 | return 0; | |
620 | } | |
621 | ||
c0a1b73c JH |
622 | static void h5_slip_delim(struct sk_buff *skb) |
623 | { | |
624 | const char delim = SLIP_DELIMITER; | |
625 | ||
59ae1d12 | 626 | skb_put_data(skb, &delim, 1); |
c0a1b73c JH |
627 | } |
628 | ||
629 | static void h5_slip_one_byte(struct sk_buff *skb, u8 c) | |
630 | { | |
631 | const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM }; | |
632 | const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC }; | |
633 | ||
634 | switch (c) { | |
635 | case SLIP_DELIMITER: | |
59ae1d12 | 636 | skb_put_data(skb, &esc_delim, 2); |
c0a1b73c JH |
637 | break; |
638 | case SLIP_ESC: | |
59ae1d12 | 639 | skb_put_data(skb, &esc_esc, 2); |
c0a1b73c JH |
640 | break; |
641 | default: | |
59ae1d12 | 642 | skb_put_data(skb, &c, 1); |
c0a1b73c JH |
643 | } |
644 | } | |
645 | ||
c826ed09 JH |
646 | static bool valid_packet_type(u8 type) |
647 | { | |
648 | switch (type) { | |
649 | case HCI_ACLDATA_PKT: | |
650 | case HCI_COMMAND_PKT: | |
651 | case HCI_SCODATA_PKT: | |
652 | case HCI_3WIRE_LINK_PKT: | |
653 | case HCI_3WIRE_ACK_PKT: | |
654 | return true; | |
655 | default: | |
656 | return false; | |
657 | } | |
658 | } | |
659 | ||
660 | static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type, | |
661 | const u8 *data, size_t len) | |
7d664fba | 662 | { |
40f10224 | 663 | struct h5 *h5 = hu->priv; |
c0a1b73c JH |
664 | struct sk_buff *nskb; |
665 | u8 hdr[4]; | |
666 | int i; | |
667 | ||
c826ed09 JH |
668 | if (!valid_packet_type(pkt_type)) { |
669 | BT_ERR("Unknown packet type %u", pkt_type); | |
670 | return NULL; | |
671 | } | |
672 | ||
c0a1b73c JH |
673 | /* |
674 | * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2 | |
675 | * (because bytes 0xc0 and 0xdb are escaped, worst case is when | |
676 | * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0 | |
677 | * delimiters at start and end). | |
678 | */ | |
679 | nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC); | |
680 | if (!nskb) | |
681 | return NULL; | |
682 | ||
618e8bc2 | 683 | hci_skb_pkt_type(nskb) = pkt_type; |
c0a1b73c JH |
684 | |
685 | h5_slip_delim(nskb); | |
686 | ||
40f10224 | 687 | hdr[0] = h5->tx_ack << 3; |
e0482103 | 688 | clear_bit(H5_TX_ACK_REQ, &h5->flags); |
c0a1b73c | 689 | |
c826ed09 JH |
690 | /* Reliable packet? */ |
691 | if (pkt_type == HCI_ACLDATA_PKT || pkt_type == HCI_COMMAND_PKT) { | |
c0a1b73c | 692 | hdr[0] |= 1 << 7; |
43eb12d7 JH |
693 | hdr[0] |= h5->tx_seq; |
694 | h5->tx_seq = (h5->tx_seq + 1) % 8; | |
c0a1b73c JH |
695 | } |
696 | ||
697 | hdr[1] = pkt_type | ((len & 0x0f) << 4); | |
698 | hdr[2] = len >> 4; | |
699 | hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff); | |
700 | ||
40f10224 JH |
701 | BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u", |
702 | hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), | |
703 | H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr), | |
704 | H5_HDR_LEN(hdr)); | |
705 | ||
c0a1b73c JH |
706 | for (i = 0; i < 4; i++) |
707 | h5_slip_one_byte(nskb, hdr[i]); | |
708 | ||
709 | for (i = 0; i < len; i++) | |
710 | h5_slip_one_byte(nskb, data[i]); | |
711 | ||
712 | h5_slip_delim(nskb); | |
713 | ||
714 | return nskb; | |
715 | } | |
716 | ||
7dec65c8 JH |
717 | static struct sk_buff *h5_dequeue(struct hci_uart *hu) |
718 | { | |
7d664fba | 719 | struct h5 *h5 = hu->priv; |
3f27e95b | 720 | unsigned long flags; |
7d664fba JH |
721 | struct sk_buff *skb, *nskb; |
722 | ||
95c5c220 JH |
723 | if (h5->sleep != H5_AWAKE) { |
724 | const unsigned char wakeup_req[] = { 0x05, 0xfa }; | |
725 | ||
726 | if (h5->sleep == H5_WAKING_UP) | |
727 | return NULL; | |
728 | ||
729 | h5->sleep = H5_WAKING_UP; | |
730 | BT_DBG("Sending wakeup request"); | |
731 | ||
732 | mod_timer(&h5->timer, jiffies + HZ / 100); | |
733 | return h5_prepare_pkt(hu, HCI_3WIRE_LINK_PKT, wakeup_req, 2); | |
734 | } | |
735 | ||
a08b15e6 | 736 | skb = skb_dequeue(&h5->unrel); |
4a2fa2b8 | 737 | if (skb) { |
618e8bc2 | 738 | nskb = h5_prepare_pkt(hu, hci_skb_pkt_type(skb), |
c0a1b73c | 739 | skb->data, skb->len); |
7d664fba JH |
740 | if (nskb) { |
741 | kfree_skb(skb); | |
742 | return nskb; | |
743 | } | |
744 | ||
745 | skb_queue_head(&h5->unrel, skb); | |
746 | BT_ERR("Could not dequeue pkt because alloc_skb failed"); | |
747 | } | |
748 | ||
3f27e95b JH |
749 | spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING); |
750 | ||
afdc944c | 751 | if (h5->unack.qlen >= h5->tx_win) |
3f27e95b JH |
752 | goto unlock; |
753 | ||
a08b15e6 | 754 | skb = skb_dequeue(&h5->rel); |
4a2fa2b8 | 755 | if (skb) { |
618e8bc2 | 756 | nskb = h5_prepare_pkt(hu, hci_skb_pkt_type(skb), |
c0a1b73c | 757 | skb->data, skb->len); |
3f27e95b JH |
758 | if (nskb) { |
759 | __skb_queue_tail(&h5->unack, skb); | |
760 | mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT); | |
761 | spin_unlock_irqrestore(&h5->unack.lock, flags); | |
762 | return nskb; | |
763 | } | |
764 | ||
765 | skb_queue_head(&h5->rel, skb); | |
766 | BT_ERR("Could not dequeue pkt because alloc_skb failed"); | |
767 | } | |
768 | ||
769 | unlock: | |
770 | spin_unlock_irqrestore(&h5->unack.lock, flags); | |
771 | ||
e0482103 | 772 | if (test_bit(H5_TX_ACK_REQ, &h5->flags)) |
40f10224 | 773 | return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0); |
7d664fba | 774 | |
7dec65c8 JH |
775 | return NULL; |
776 | } | |
777 | ||
778 | static int h5_flush(struct hci_uart *hu) | |
779 | { | |
7d664fba JH |
780 | BT_DBG("hu %p", hu); |
781 | return 0; | |
7dec65c8 JH |
782 | } |
783 | ||
4ee7ef19 | 784 | static const struct hci_uart_proto h5p = { |
7dec65c8 | 785 | .id = HCI_UART_3WIRE, |
7c40fb8d | 786 | .name = "Three-wire (H5)", |
7dec65c8 JH |
787 | .open = h5_open, |
788 | .close = h5_close, | |
4eb3cbc4 | 789 | .setup = h5_setup, |
7dec65c8 JH |
790 | .recv = h5_recv, |
791 | .enqueue = h5_enqueue, | |
792 | .dequeue = h5_dequeue, | |
793 | .flush = h5_flush, | |
794 | }; | |
795 | ||
ce945552 HG |
796 | static int h5_serdev_probe(struct serdev_device *serdev) |
797 | { | |
4eb3cbc4 | 798 | const struct acpi_device_id *match; |
ce945552 HG |
799 | struct device *dev = &serdev->dev; |
800 | struct h5 *h5; | |
801 | ||
802 | h5 = devm_kzalloc(dev, sizeof(*h5), GFP_KERNEL); | |
803 | if (!h5) | |
804 | return -ENOMEM; | |
805 | ||
806 | set_bit(HCI_UART_RESET_ON_INIT, &h5->serdev_hu.flags); | |
807 | ||
808 | h5->hu = &h5->serdev_hu; | |
809 | h5->serdev_hu.serdev = serdev; | |
810 | serdev_device_set_drvdata(serdev, h5); | |
811 | ||
4eb3cbc4 JC |
812 | if (has_acpi_companion(dev)) { |
813 | match = acpi_match_device(dev->driver->acpi_match_table, dev); | |
814 | if (!match) | |
815 | return -ENODEV; | |
816 | ||
817 | h5->vnd = (const struct h5_vnd *)match->driver_data; | |
818 | h5->id = (char *)match->id; | |
4c791489 HG |
819 | |
820 | if (h5->vnd->acpi_gpio_map) | |
821 | devm_acpi_dev_add_driver_gpios(dev, | |
822 | h5->vnd->acpi_gpio_map); | |
4eb3cbc4 JC |
823 | } |
824 | ||
4c791489 HG |
825 | h5->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW); |
826 | if (IS_ERR(h5->enable_gpio)) | |
827 | return PTR_ERR(h5->enable_gpio); | |
828 | ||
829 | h5->device_wake_gpio = devm_gpiod_get_optional(dev, "device-wake", | |
830 | GPIOD_OUT_LOW); | |
831 | if (IS_ERR(h5->device_wake_gpio)) | |
832 | return PTR_ERR(h5->device_wake_gpio); | |
833 | ||
ce945552 HG |
834 | return hci_uart_register_device(&h5->serdev_hu, &h5p); |
835 | } | |
836 | ||
837 | static void h5_serdev_remove(struct serdev_device *serdev) | |
838 | { | |
839 | struct h5 *h5 = serdev_device_get_drvdata(serdev); | |
840 | ||
841 | hci_uart_unregister_device(&h5->serdev_hu); | |
842 | } | |
843 | ||
b9763cdf | 844 | #ifdef CONFIG_BT_HCIUART_RTL |
b825d7c4 JC |
845 | static int h5_btrtl_setup(struct h5 *h5) |
846 | { | |
847 | struct btrtl_device_info *btrtl_dev; | |
848 | struct sk_buff *skb; | |
849 | __le32 baudrate_data; | |
850 | u32 device_baudrate; | |
851 | unsigned int controller_baudrate; | |
852 | bool flow_control; | |
853 | int err; | |
854 | ||
855 | btrtl_dev = btrtl_initialize(h5->hu->hdev, h5->id); | |
856 | if (IS_ERR(btrtl_dev)) | |
857 | return PTR_ERR(btrtl_dev); | |
858 | ||
859 | err = btrtl_get_uart_settings(h5->hu->hdev, btrtl_dev, | |
860 | &controller_baudrate, &device_baudrate, | |
861 | &flow_control); | |
862 | if (err) | |
863 | goto out_free; | |
864 | ||
865 | baudrate_data = cpu_to_le32(device_baudrate); | |
866 | skb = __hci_cmd_sync(h5->hu->hdev, 0xfc17, sizeof(baudrate_data), | |
867 | &baudrate_data, HCI_INIT_TIMEOUT); | |
868 | if (IS_ERR(skb)) { | |
869 | rtl_dev_err(h5->hu->hdev, "set baud rate command failed\n"); | |
870 | err = PTR_ERR(skb); | |
871 | goto out_free; | |
872 | } else { | |
873 | kfree_skb(skb); | |
874 | } | |
875 | /* Give the device some time to set up the new baudrate. */ | |
876 | usleep_range(10000, 20000); | |
877 | ||
878 | serdev_device_set_baudrate(h5->hu->serdev, controller_baudrate); | |
879 | serdev_device_set_flow_control(h5->hu->serdev, flow_control); | |
880 | ||
881 | err = btrtl_download_firmware(h5->hu->hdev, btrtl_dev); | |
882 | /* Give the device some time before the hci-core sends it a reset */ | |
883 | usleep_range(10000, 20000); | |
884 | ||
885 | out_free: | |
886 | btrtl_free(btrtl_dev); | |
887 | ||
888 | return err; | |
889 | } | |
890 | ||
891 | static void h5_btrtl_open(struct h5 *h5) | |
892 | { | |
893 | /* Devices always start with these fixed parameters */ | |
894 | serdev_device_set_flow_control(h5->hu->serdev, false); | |
895 | serdev_device_set_parity(h5->hu->serdev, SERDEV_PARITY_EVEN); | |
896 | serdev_device_set_baudrate(h5->hu->serdev, 115200); | |
4c791489 HG |
897 | |
898 | /* The controller needs up to 500ms to wakeup */ | |
899 | gpiod_set_value_cansleep(h5->enable_gpio, 1); | |
900 | gpiod_set_value_cansleep(h5->device_wake_gpio, 1); | |
901 | msleep(500); | |
b825d7c4 JC |
902 | } |
903 | ||
4c791489 HG |
904 | static void h5_btrtl_close(struct h5 *h5) |
905 | { | |
906 | gpiod_set_value_cansleep(h5->device_wake_gpio, 0); | |
907 | gpiod_set_value_cansleep(h5->enable_gpio, 0); | |
908 | } | |
909 | ||
910 | static const struct acpi_gpio_params btrtl_device_wake_gpios = { 0, 0, false }; | |
911 | static const struct acpi_gpio_params btrtl_enable_gpios = { 1, 0, false }; | |
912 | static const struct acpi_gpio_params btrtl_host_wake_gpios = { 2, 0, false }; | |
913 | static const struct acpi_gpio_mapping acpi_btrtl_gpios[] = { | |
914 | { "device-wake-gpios", &btrtl_device_wake_gpios, 1 }, | |
915 | { "enable-gpios", &btrtl_enable_gpios, 1 }, | |
916 | { "host-wake-gpios", &btrtl_host_wake_gpios, 1 }, | |
917 | {}, | |
918 | }; | |
919 | ||
b825d7c4 JC |
920 | static struct h5_vnd rtl_vnd = { |
921 | .setup = h5_btrtl_setup, | |
922 | .open = h5_btrtl_open, | |
4c791489 HG |
923 | .close = h5_btrtl_close, |
924 | .acpi_gpio_map = acpi_btrtl_gpios, | |
b825d7c4 | 925 | }; |
b9763cdf | 926 | #endif |
b825d7c4 JC |
927 | |
928 | #ifdef CONFIG_ACPI | |
929 | static const struct acpi_device_id h5_acpi_match[] = { | |
b9763cdf | 930 | #ifdef CONFIG_BT_HCIUART_RTL |
b825d7c4 | 931 | { "OBDA8723", (kernel_ulong_t)&rtl_vnd }, |
b9763cdf | 932 | #endif |
b825d7c4 JC |
933 | { }, |
934 | }; | |
935 | MODULE_DEVICE_TABLE(acpi, h5_acpi_match); | |
936 | #endif | |
937 | ||
ce945552 HG |
938 | static struct serdev_device_driver h5_serdev_driver = { |
939 | .probe = h5_serdev_probe, | |
940 | .remove = h5_serdev_remove, | |
941 | .driver = { | |
942 | .name = "hci_uart_h5", | |
b825d7c4 | 943 | .acpi_match_table = ACPI_PTR(h5_acpi_match), |
ce945552 HG |
944 | }, |
945 | }; | |
946 | ||
7dec65c8 JH |
947 | int __init h5_init(void) |
948 | { | |
ce945552 | 949 | serdev_device_driver_register(&h5_serdev_driver); |
01009eec | 950 | return hci_uart_register_proto(&h5p); |
7dec65c8 JH |
951 | } |
952 | ||
953 | int __exit h5_deinit(void) | |
954 | { | |
ce945552 | 955 | serdev_device_driver_unregister(&h5_serdev_driver); |
7dec65c8 JH |
956 | return hci_uart_unregister_proto(&h5p); |
957 | } |