blob: f50afb2fb3646972f55a39a74231a0ebc6a355b6 [file] [log] [blame]
Johan Hedberg7dec65c2012-07-16 16:12:02 +03001/*
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
24#include <linux/kernel.h>
25#include <linux/errno.h>
26#include <linux/skbuff.h>
27
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30
31#include "hci_uart.h"
32
Johan Hedbergc0a1b732012-07-16 16:12:06 +030033#define HCI_3WIRE_ACK_PKT 0
34#define HCI_3WIRE_LINK_PKT 15
35
Johan Hedberg3f27e952012-07-16 16:12:04 +030036#define H5_TXWINSIZE 4
37
38#define H5_ACK_TIMEOUT msecs_to_jiffies(250)
39
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030040/*
41 * Maximum Three-wire packet:
42 * 4 byte header + max value for 12-bit length + 2 bytes for CRC
43 */
44#define H5_MAX_LEN (4 + 0xfff + 2)
45
Johan Hedberg01977c02012-07-16 16:12:07 +030046/* Convenience macros for reading Three-wire header values */
47#define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07)
48#define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07)
49#define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01)
50#define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01)
51#define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f)
52#define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0xff) + ((hdr)[2] << 4))
53
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030054#define SLIP_DELIMITER 0xc0
55#define SLIP_ESC 0xdb
56#define SLIP_ESC_DELIM 0xdc
57#define SLIP_ESC_ESC 0xdd
58
Johan Hedberg7d664fb2012-07-16 16:12:03 +030059struct h5 {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030060 struct sk_buff_head unack; /* Unack'ed packets queue */
61 struct sk_buff_head rel; /* Reliable packets queue */
62 struct sk_buff_head unrel; /* Unreliable packets queue */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030063
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030064 struct sk_buff *rx_skb; /* Receive buffer */
65 size_t rx_pending; /* Expecting more bytes */
66 bool rx_esc; /* SLIP escape mode */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030067
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030068 int (*rx_func) (struct hci_uart *hu, u8 c);
Johan Hedberg3f27e952012-07-16 16:12:04 +030069
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030070 struct timer_list timer; /* Retransmission timer */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030071
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030072 bool txack_req;
73
Johan Hedbergc0a1b732012-07-16 16:12:06 +030074 u8 next_ack;
75 u8 next_seq;
Johan Hedberg7d664fb2012-07-16 16:12:03 +030076};
77
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030078static void h5_reset_rx(struct h5 *h5);
79
Johan Hedberg3f27e952012-07-16 16:12:04 +030080static void h5_timed_event(unsigned long arg)
81{
82 struct hci_uart *hu = (struct hci_uart *) arg;
83 struct h5 *h5 = hu->priv;
84 struct sk_buff *skb;
85 unsigned long flags;
86
87 BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
88
89 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
90
91 while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
Johan Hedbergc0a1b732012-07-16 16:12:06 +030092 h5->next_seq = (h5->next_seq - 1) & 0x07;
Johan Hedberg3f27e952012-07-16 16:12:04 +030093 skb_queue_head(&h5->rel, skb);
94 }
95
96 spin_unlock_irqrestore(&h5->unack.lock, flags);
97
98 hci_uart_tx_wakeup(hu);
99}
100
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300101static int h5_open(struct hci_uart *hu)
102{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300103 struct h5 *h5;
104
105 BT_DBG("hu %p", hu);
106
107 h5 = kzalloc(sizeof(*h5), GFP_KERNEL);
108 if (!h5)
109 return -ENOMEM;
110
111 hu->priv = h5;
112
113 skb_queue_head_init(&h5->unack);
114 skb_queue_head_init(&h5->rel);
115 skb_queue_head_init(&h5->unrel);
116
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300117 h5_reset_rx(h5);
118
Johan Hedberg3f27e952012-07-16 16:12:04 +0300119 init_timer(&h5->timer);
120 h5->timer.function = h5_timed_event;
121 h5->timer.data = (unsigned long) hu;
122
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300123 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300124}
125
126static int h5_close(struct hci_uart *hu)
127{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300128 struct h5 *h5 = hu->priv;
129
130 skb_queue_purge(&h5->unack);
131 skb_queue_purge(&h5->rel);
132 skb_queue_purge(&h5->unrel);
133
Johan Hedberg3f27e952012-07-16 16:12:04 +0300134 del_timer(&h5->timer);
135
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300136 kfree(h5);
137
138 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300139}
140
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300141static void h5_handle_internal_rx(struct hci_uart *hu)
142{
143 BT_DBG("%s", hu->hdev->name);
144}
145
146static void h5_complete_rx_pkt(struct hci_uart *hu)
147{
148 struct h5 *h5 = hu->priv;
149 u8 pkt_type;
150
151 BT_DBG("%s", hu->hdev->name);
152
153 pkt_type = h5->rx_skb->data[1] & 0x0f;
154
155 switch (pkt_type) {
156 case HCI_EVENT_PKT:
157 case HCI_ACLDATA_PKT:
158 case HCI_SCODATA_PKT:
159 bt_cb(h5->rx_skb)->pkt_type = pkt_type;
160
161 /* Remove Three-wire header */
162 skb_pull(h5->rx_skb, 4);
163
164 hci_recv_frame(h5->rx_skb);
165 h5->rx_skb = NULL;
166
167 break;
168
169 default:
170 h5_handle_internal_rx(hu);
171 break;
172 }
173
174 h5_reset_rx(h5);
175}
176
177static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
178{
179 struct h5 *h5 = hu->priv;
180
181 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
182
183 h5_complete_rx_pkt(hu);
184 h5_reset_rx(h5);
185
186 return 0;
187}
188
189static int h5_rx_payload(struct hci_uart *hu, unsigned char c)
190{
191 struct h5 *h5 = hu->priv;
192 const unsigned char *hdr = h5->rx_skb->data;
193
194 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
195
196 if ((hdr[0] >> 4) & 0x01) {
197 h5->rx_func = h5_rx_crc;
198 h5->rx_pending = 2;
199 } else {
200 h5_complete_rx_pkt(hu);
201 h5_reset_rx(h5);
202 }
203
204 return 0;
205}
206
207static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c)
208{
209 struct h5 *h5 = hu->priv;
210 const unsigned char *hdr = h5->rx_skb->data;
211
212 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
213
214 if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
215 BT_ERR("Invalid header checksum");
216 h5_reset_rx(h5);
217 return 0;
218 }
219
220 h5->rx_func = h5_rx_payload;
221 h5->rx_pending = ((hdr[1] >> 4) & 0xff) + (hdr[2] << 4);
222
223 return 0;
224}
225
226static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
227{
228 struct h5 *h5 = hu->priv;
229
230 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
231
232 if (c == SLIP_DELIMITER)
233 return 1;
234
235 h5->rx_func = h5_rx_3wire_hdr;
236 h5->rx_pending = 4;
237
238 h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC);
239 if (!h5->rx_skb) {
240 BT_ERR("Can't allocate mem for new packet");
241 h5_reset_rx(h5);
242 return -ENOMEM;
243 }
244
245 h5->rx_skb->dev = (void *) hu->hdev;
246
247 return 0;
248}
249
250static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
251{
252 struct h5 *h5 = hu->priv;
253
254 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
255
256 if (c == SLIP_DELIMITER)
257 h5->rx_func = h5_rx_pkt_start;
258
259 return 1;
260}
261
262static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
263{
264 const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC;
265 const u8 *byte = &c;
266
267 if (!h5->rx_esc && c == SLIP_ESC) {
268 h5->rx_esc = true;
269 return;
270 }
271
272 if (h5->rx_esc) {
273 switch (c) {
274 case SLIP_ESC_DELIM:
275 byte = &delim;
276 break;
277 case SLIP_ESC_ESC:
278 byte = &esc;
279 break;
280 default:
281 BT_ERR("Invalid esc byte 0x%02hhx", c);
282 h5_reset_rx(h5);
283 return;
284 }
285
286 h5->rx_esc = false;
287 }
288
289 memcpy(skb_put(h5->rx_skb, 1), byte, 1);
290 h5->rx_pending--;
291
292 BT_DBG("unsliped 0x%02hhx", *byte);
293}
294
295static void h5_reset_rx(struct h5 *h5)
296{
297 if (h5->rx_skb) {
298 kfree_skb(h5->rx_skb);
299 h5->rx_skb = NULL;
300 }
301
302 h5->rx_func = h5_rx_delimiter;
303 h5->rx_pending = 0;
304 h5->rx_esc = false;
305}
306
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300307static int h5_recv(struct hci_uart *hu, void *data, int count)
308{
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300309 struct h5 *h5 = hu->priv;
310 unsigned char *ptr = data;
311
312 BT_DBG("%s count %d", hu->hdev->name, count);
313
314 while (count > 0) {
315 int processed;
316
317 if (h5->rx_pending > 0) {
318 if (*ptr == SLIP_DELIMITER) {
319 BT_ERR("Too short H5 packet");
320 h5_reset_rx(h5);
321 continue;
322 }
323
324 h5_unslip_one_byte(h5, *ptr);
325
326 ptr++; count--;
327 continue;
328 }
329
330 processed = h5->rx_func(hu, *ptr);
331 if (processed < 0)
332 return processed;
333
334 ptr += processed;
335 count -= processed;
336 }
337
338 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300339}
340
341static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
342{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300343 struct h5 *h5 = hu->priv;
344
345 if (skb->len > 0xfff) {
346 BT_ERR("Packet too long (%u bytes)", skb->len);
347 kfree_skb(skb);
348 return 0;
349 }
350
351 switch (bt_cb(skb)->pkt_type) {
352 case HCI_ACLDATA_PKT:
353 case HCI_COMMAND_PKT:
354 skb_queue_tail(&h5->rel, skb);
355 break;
356
357 case HCI_SCODATA_PKT:
358 skb_queue_tail(&h5->unrel, skb);
359 break;
360
361 default:
362 BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type);
363 kfree_skb(skb);
364 break;
365 }
366
367 return 0;
368}
369
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300370static void h5_slip_delim(struct sk_buff *skb)
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300371{
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300372 const char delim = SLIP_DELIMITER;
373
374 memcpy(skb_put(skb, 1), &delim, 1);
375}
376
377static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
378{
379 const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM };
380 const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC };
381
382 switch (c) {
383 case SLIP_DELIMITER:
384 memcpy(skb_put(skb, 2), &esc_delim, 2);
385 break;
386 case SLIP_ESC:
387 memcpy(skb_put(skb, 2), &esc_esc, 2);
388 break;
389 default:
390 memcpy(skb_put(skb, 1), &c, 1);
391 }
392}
393
394static struct sk_buff *h5_build_pkt(struct h5 *h5, bool rel, u8 pkt_type,
395 const u8 *data, size_t len)
396{
397 struct sk_buff *nskb;
398 u8 hdr[4];
399 int i;
400
401 /*
402 * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
403 * (because bytes 0xc0 and 0xdb are escaped, worst case is when
404 * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
405 * delimiters at start and end).
406 */
407 nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
408 if (!nskb)
409 return NULL;
410
411 bt_cb(nskb)->pkt_type = pkt_type;
412
413 h5_slip_delim(nskb);
414
415 hdr[0] = h5->next_ack << 3;
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300416 h5->txack_req = false;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300417
418 if (rel) {
419 hdr[0] |= 1 << 7;
420 hdr[0] |= h5->next_seq;
421 h5->next_seq = (h5->next_seq + 1) % 8;
422 }
423
424 hdr[1] = pkt_type | ((len & 0x0f) << 4);
425 hdr[2] = len >> 4;
426 hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
427
428 for (i = 0; i < 4; i++)
429 h5_slip_one_byte(nskb, hdr[i]);
430
431 for (i = 0; i < len; i++)
432 h5_slip_one_byte(nskb, data[i]);
433
434 h5_slip_delim(nskb);
435
436 return nskb;
437}
438
439static struct sk_buff *h5_prepare_pkt(struct h5 *h5, u8 pkt_type,
440 const u8 *data, size_t len)
441{
442 bool rel;
443
444 switch (pkt_type) {
445 case HCI_ACLDATA_PKT:
446 case HCI_COMMAND_PKT:
447 rel = true;
448 break;
449 case HCI_SCODATA_PKT:
450 case HCI_3WIRE_LINK_PKT:
451 case HCI_3WIRE_ACK_PKT:
452 rel = false;
453 break;
454 default:
455 BT_ERR("Unknown packet type %u", pkt_type);
456 return NULL;
457 }
458
459 return h5_build_pkt(h5, rel, pkt_type, data, len);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300460}
461
462static struct sk_buff *h5_prepare_ack(struct h5 *h5)
463{
464 h5->txack_req = false;
465 return NULL;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300466}
467
468static struct sk_buff *h5_dequeue(struct hci_uart *hu)
469{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300470 struct h5 *h5 = hu->priv;
Johan Hedberg3f27e952012-07-16 16:12:04 +0300471 unsigned long flags;
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300472 struct sk_buff *skb, *nskb;
473
474 if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300475 nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type,
476 skb->data, skb->len);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300477 if (nskb) {
478 kfree_skb(skb);
479 return nskb;
480 }
481
482 skb_queue_head(&h5->unrel, skb);
483 BT_ERR("Could not dequeue pkt because alloc_skb failed");
484 }
485
Johan Hedberg3f27e952012-07-16 16:12:04 +0300486 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
487
488 if (h5->unack.qlen >= H5_TXWINSIZE)
489 goto unlock;
490
491 if ((skb = skb_dequeue(&h5->rel)) != NULL) {
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300492 nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type,
493 skb->data, skb->len);
Johan Hedberg3f27e952012-07-16 16:12:04 +0300494 if (nskb) {
495 __skb_queue_tail(&h5->unack, skb);
496 mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
497 spin_unlock_irqrestore(&h5->unack.lock, flags);
498 return nskb;
499 }
500
501 skb_queue_head(&h5->rel, skb);
502 BT_ERR("Could not dequeue pkt because alloc_skb failed");
503 }
504
505unlock:
506 spin_unlock_irqrestore(&h5->unack.lock, flags);
507
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300508 if (h5->txack_req)
509 return h5_prepare_ack(h5);
510
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300511 return NULL;
512}
513
514static int h5_flush(struct hci_uart *hu)
515{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300516 BT_DBG("hu %p", hu);
517 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300518}
519
520static struct hci_uart_proto h5p = {
521 .id = HCI_UART_3WIRE,
522 .open = h5_open,
523 .close = h5_close,
524 .recv = h5_recv,
525 .enqueue = h5_enqueue,
526 .dequeue = h5_dequeue,
527 .flush = h5_flush,
528};
529
530int __init h5_init(void)
531{
532 int err = hci_uart_register_proto(&h5p);
533
534 if (!err)
535 BT_INFO("HCI Three-wire UART (H5) protocol initialized");
536 else
537 BT_ERR("HCI Three-wire UART (H5) protocol init failed");
538
539 return err;
540}
541
542int __exit h5_deinit(void)
543{
544 return hci_uart_unregister_proto(&h5p);
545}