blob: 6df4c07bbd7b75025d4d6246e362b278db13e38e [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
46#define SLIP_DELIMITER 0xc0
47#define SLIP_ESC 0xdb
48#define SLIP_ESC_DELIM 0xdc
49#define SLIP_ESC_ESC 0xdd
50
Johan Hedberg7d664fb2012-07-16 16:12:03 +030051struct h5 {
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030052 struct sk_buff_head unack; /* Unack'ed packets queue */
53 struct sk_buff_head rel; /* Reliable packets queue */
54 struct sk_buff_head unrel; /* Unreliable packets queue */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030055
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030056 struct sk_buff *rx_skb; /* Receive buffer */
57 size_t rx_pending; /* Expecting more bytes */
58 bool rx_esc; /* SLIP escape mode */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030059
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030060 int (*rx_func) (struct hci_uart *hu, u8 c);
Johan Hedberg3f27e952012-07-16 16:12:04 +030061
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030062 struct timer_list timer; /* Retransmission timer */
Johan Hedberg7d664fb2012-07-16 16:12:03 +030063
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030064 bool txack_req;
65
Johan Hedbergc0a1b732012-07-16 16:12:06 +030066 u8 next_ack;
67 u8 next_seq;
Johan Hedberg7d664fb2012-07-16 16:12:03 +030068};
69
Johan Hedbergbc1f35b2012-07-16 16:12:05 +030070static void h5_reset_rx(struct h5 *h5);
71
Johan Hedberg3f27e952012-07-16 16:12:04 +030072static void h5_timed_event(unsigned long arg)
73{
74 struct hci_uart *hu = (struct hci_uart *) arg;
75 struct h5 *h5 = hu->priv;
76 struct sk_buff *skb;
77 unsigned long flags;
78
79 BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
80
81 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
82
83 while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
Johan Hedbergc0a1b732012-07-16 16:12:06 +030084 h5->next_seq = (h5->next_seq - 1) & 0x07;
Johan Hedberg3f27e952012-07-16 16:12:04 +030085 skb_queue_head(&h5->rel, skb);
86 }
87
88 spin_unlock_irqrestore(&h5->unack.lock, flags);
89
90 hci_uart_tx_wakeup(hu);
91}
92
Johan Hedberg7dec65c2012-07-16 16:12:02 +030093static int h5_open(struct hci_uart *hu)
94{
Johan Hedberg7d664fb2012-07-16 16:12:03 +030095 struct h5 *h5;
96
97 BT_DBG("hu %p", hu);
98
99 h5 = kzalloc(sizeof(*h5), GFP_KERNEL);
100 if (!h5)
101 return -ENOMEM;
102
103 hu->priv = h5;
104
105 skb_queue_head_init(&h5->unack);
106 skb_queue_head_init(&h5->rel);
107 skb_queue_head_init(&h5->unrel);
108
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300109 h5_reset_rx(h5);
110
Johan Hedberg3f27e952012-07-16 16:12:04 +0300111 init_timer(&h5->timer);
112 h5->timer.function = h5_timed_event;
113 h5->timer.data = (unsigned long) hu;
114
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300115 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300116}
117
118static int h5_close(struct hci_uart *hu)
119{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300120 struct h5 *h5 = hu->priv;
121
122 skb_queue_purge(&h5->unack);
123 skb_queue_purge(&h5->rel);
124 skb_queue_purge(&h5->unrel);
125
Johan Hedberg3f27e952012-07-16 16:12:04 +0300126 del_timer(&h5->timer);
127
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300128 kfree(h5);
129
130 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300131}
132
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300133static void h5_handle_internal_rx(struct hci_uart *hu)
134{
135 BT_DBG("%s", hu->hdev->name);
136}
137
138static void h5_complete_rx_pkt(struct hci_uart *hu)
139{
140 struct h5 *h5 = hu->priv;
141 u8 pkt_type;
142
143 BT_DBG("%s", hu->hdev->name);
144
145 pkt_type = h5->rx_skb->data[1] & 0x0f;
146
147 switch (pkt_type) {
148 case HCI_EVENT_PKT:
149 case HCI_ACLDATA_PKT:
150 case HCI_SCODATA_PKT:
151 bt_cb(h5->rx_skb)->pkt_type = pkt_type;
152
153 /* Remove Three-wire header */
154 skb_pull(h5->rx_skb, 4);
155
156 hci_recv_frame(h5->rx_skb);
157 h5->rx_skb = NULL;
158
159 break;
160
161 default:
162 h5_handle_internal_rx(hu);
163 break;
164 }
165
166 h5_reset_rx(h5);
167}
168
169static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
170{
171 struct h5 *h5 = hu->priv;
172
173 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
174
175 h5_complete_rx_pkt(hu);
176 h5_reset_rx(h5);
177
178 return 0;
179}
180
181static int h5_rx_payload(struct hci_uart *hu, unsigned char c)
182{
183 struct h5 *h5 = hu->priv;
184 const unsigned char *hdr = h5->rx_skb->data;
185
186 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
187
188 if ((hdr[0] >> 4) & 0x01) {
189 h5->rx_func = h5_rx_crc;
190 h5->rx_pending = 2;
191 } else {
192 h5_complete_rx_pkt(hu);
193 h5_reset_rx(h5);
194 }
195
196 return 0;
197}
198
199static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c)
200{
201 struct h5 *h5 = hu->priv;
202 const unsigned char *hdr = h5->rx_skb->data;
203
204 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
205
206 if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
207 BT_ERR("Invalid header checksum");
208 h5_reset_rx(h5);
209 return 0;
210 }
211
212 h5->rx_func = h5_rx_payload;
213 h5->rx_pending = ((hdr[1] >> 4) & 0xff) + (hdr[2] << 4);
214
215 return 0;
216}
217
218static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
219{
220 struct h5 *h5 = hu->priv;
221
222 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
223
224 if (c == SLIP_DELIMITER)
225 return 1;
226
227 h5->rx_func = h5_rx_3wire_hdr;
228 h5->rx_pending = 4;
229
230 h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC);
231 if (!h5->rx_skb) {
232 BT_ERR("Can't allocate mem for new packet");
233 h5_reset_rx(h5);
234 return -ENOMEM;
235 }
236
237 h5->rx_skb->dev = (void *) hu->hdev;
238
239 return 0;
240}
241
242static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
243{
244 struct h5 *h5 = hu->priv;
245
246 BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
247
248 if (c == SLIP_DELIMITER)
249 h5->rx_func = h5_rx_pkt_start;
250
251 return 1;
252}
253
254static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
255{
256 const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC;
257 const u8 *byte = &c;
258
259 if (!h5->rx_esc && c == SLIP_ESC) {
260 h5->rx_esc = true;
261 return;
262 }
263
264 if (h5->rx_esc) {
265 switch (c) {
266 case SLIP_ESC_DELIM:
267 byte = &delim;
268 break;
269 case SLIP_ESC_ESC:
270 byte = &esc;
271 break;
272 default:
273 BT_ERR("Invalid esc byte 0x%02hhx", c);
274 h5_reset_rx(h5);
275 return;
276 }
277
278 h5->rx_esc = false;
279 }
280
281 memcpy(skb_put(h5->rx_skb, 1), byte, 1);
282 h5->rx_pending--;
283
284 BT_DBG("unsliped 0x%02hhx", *byte);
285}
286
287static void h5_reset_rx(struct h5 *h5)
288{
289 if (h5->rx_skb) {
290 kfree_skb(h5->rx_skb);
291 h5->rx_skb = NULL;
292 }
293
294 h5->rx_func = h5_rx_delimiter;
295 h5->rx_pending = 0;
296 h5->rx_esc = false;
297}
298
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300299static int h5_recv(struct hci_uart *hu, void *data, int count)
300{
Johan Hedbergbc1f35b2012-07-16 16:12:05 +0300301 struct h5 *h5 = hu->priv;
302 unsigned char *ptr = data;
303
304 BT_DBG("%s count %d", hu->hdev->name, count);
305
306 while (count > 0) {
307 int processed;
308
309 if (h5->rx_pending > 0) {
310 if (*ptr == SLIP_DELIMITER) {
311 BT_ERR("Too short H5 packet");
312 h5_reset_rx(h5);
313 continue;
314 }
315
316 h5_unslip_one_byte(h5, *ptr);
317
318 ptr++; count--;
319 continue;
320 }
321
322 processed = h5->rx_func(hu, *ptr);
323 if (processed < 0)
324 return processed;
325
326 ptr += processed;
327 count -= processed;
328 }
329
330 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300331}
332
333static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
334{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300335 struct h5 *h5 = hu->priv;
336
337 if (skb->len > 0xfff) {
338 BT_ERR("Packet too long (%u bytes)", skb->len);
339 kfree_skb(skb);
340 return 0;
341 }
342
343 switch (bt_cb(skb)->pkt_type) {
344 case HCI_ACLDATA_PKT:
345 case HCI_COMMAND_PKT:
346 skb_queue_tail(&h5->rel, skb);
347 break;
348
349 case HCI_SCODATA_PKT:
350 skb_queue_tail(&h5->unrel, skb);
351 break;
352
353 default:
354 BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type);
355 kfree_skb(skb);
356 break;
357 }
358
359 return 0;
360}
361
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300362static void h5_slip_delim(struct sk_buff *skb)
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300363{
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300364 const char delim = SLIP_DELIMITER;
365
366 memcpy(skb_put(skb, 1), &delim, 1);
367}
368
369static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
370{
371 const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM };
372 const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC };
373
374 switch (c) {
375 case SLIP_DELIMITER:
376 memcpy(skb_put(skb, 2), &esc_delim, 2);
377 break;
378 case SLIP_ESC:
379 memcpy(skb_put(skb, 2), &esc_esc, 2);
380 break;
381 default:
382 memcpy(skb_put(skb, 1), &c, 1);
383 }
384}
385
386static struct sk_buff *h5_build_pkt(struct h5 *h5, bool rel, u8 pkt_type,
387 const u8 *data, size_t len)
388{
389 struct sk_buff *nskb;
390 u8 hdr[4];
391 int i;
392
393 /*
394 * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
395 * (because bytes 0xc0 and 0xdb are escaped, worst case is when
396 * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
397 * delimiters at start and end).
398 */
399 nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
400 if (!nskb)
401 return NULL;
402
403 bt_cb(nskb)->pkt_type = pkt_type;
404
405 h5_slip_delim(nskb);
406
407 hdr[0] = h5->next_ack << 3;
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300408 h5->txack_req = false;
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300409
410 if (rel) {
411 hdr[0] |= 1 << 7;
412 hdr[0] |= h5->next_seq;
413 h5->next_seq = (h5->next_seq + 1) % 8;
414 }
415
416 hdr[1] = pkt_type | ((len & 0x0f) << 4);
417 hdr[2] = len >> 4;
418 hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
419
420 for (i = 0; i < 4; i++)
421 h5_slip_one_byte(nskb, hdr[i]);
422
423 for (i = 0; i < len; i++)
424 h5_slip_one_byte(nskb, data[i]);
425
426 h5_slip_delim(nskb);
427
428 return nskb;
429}
430
431static struct sk_buff *h5_prepare_pkt(struct h5 *h5, u8 pkt_type,
432 const u8 *data, size_t len)
433{
434 bool rel;
435
436 switch (pkt_type) {
437 case HCI_ACLDATA_PKT:
438 case HCI_COMMAND_PKT:
439 rel = true;
440 break;
441 case HCI_SCODATA_PKT:
442 case HCI_3WIRE_LINK_PKT:
443 case HCI_3WIRE_ACK_PKT:
444 rel = false;
445 break;
446 default:
447 BT_ERR("Unknown packet type %u", pkt_type);
448 return NULL;
449 }
450
451 return h5_build_pkt(h5, rel, pkt_type, data, len);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300452}
453
454static struct sk_buff *h5_prepare_ack(struct h5 *h5)
455{
456 h5->txack_req = false;
457 return NULL;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300458}
459
460static struct sk_buff *h5_dequeue(struct hci_uart *hu)
461{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300462 struct h5 *h5 = hu->priv;
Johan Hedberg3f27e952012-07-16 16:12:04 +0300463 unsigned long flags;
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300464 struct sk_buff *skb, *nskb;
465
466 if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300467 nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type,
468 skb->data, skb->len);
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300469 if (nskb) {
470 kfree_skb(skb);
471 return nskb;
472 }
473
474 skb_queue_head(&h5->unrel, skb);
475 BT_ERR("Could not dequeue pkt because alloc_skb failed");
476 }
477
Johan Hedberg3f27e952012-07-16 16:12:04 +0300478 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
479
480 if (h5->unack.qlen >= H5_TXWINSIZE)
481 goto unlock;
482
483 if ((skb = skb_dequeue(&h5->rel)) != NULL) {
Johan Hedbergc0a1b732012-07-16 16:12:06 +0300484 nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type,
485 skb->data, skb->len);
Johan Hedberg3f27e952012-07-16 16:12:04 +0300486 if (nskb) {
487 __skb_queue_tail(&h5->unack, skb);
488 mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
489 spin_unlock_irqrestore(&h5->unack.lock, flags);
490 return nskb;
491 }
492
493 skb_queue_head(&h5->rel, skb);
494 BT_ERR("Could not dequeue pkt because alloc_skb failed");
495 }
496
497unlock:
498 spin_unlock_irqrestore(&h5->unack.lock, flags);
499
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300500 if (h5->txack_req)
501 return h5_prepare_ack(h5);
502
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300503 return NULL;
504}
505
506static int h5_flush(struct hci_uart *hu)
507{
Johan Hedberg7d664fb2012-07-16 16:12:03 +0300508 BT_DBG("hu %p", hu);
509 return 0;
Johan Hedberg7dec65c2012-07-16 16:12:02 +0300510}
511
512static struct hci_uart_proto h5p = {
513 .id = HCI_UART_3WIRE,
514 .open = h5_open,
515 .close = h5_close,
516 .recv = h5_recv,
517 .enqueue = h5_enqueue,
518 .dequeue = h5_dequeue,
519 .flush = h5_flush,
520};
521
522int __init h5_init(void)
523{
524 int err = hci_uart_register_proto(&h5p);
525
526 if (!err)
527 BT_INFO("HCI Three-wire UART (H5) protocol initialized");
528 else
529 BT_ERR("HCI Three-wire UART (H5) protocol init failed");
530
531 return err;
532}
533
534int __exit h5_deinit(void)
535{
536 return hci_uart_unregister_proto(&h5p);
537}