patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / bluetooth / hci_bcsp.c
1 /* 
2    BlueCore Serial Protocol (BCSP) for Linux Bluetooth stack (BlueZ).
3    Copyright 2002 by Fabrizio Gennari <fabrizio.gennari@philips.com>
4
5    Based on
6        hci_h4.c  by Maxim Krasnyansky <maxk@qualcomm.com>
7        ABCSP     by Carl Orsborn <cjo@csr.com>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License version 2 as
11    published by the Free Software Foundation;
12
13    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
16    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
18    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
19    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
20    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
22    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
23    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
24    SOFTWARE IS DISCLAIMED.
25 */
26
27 /*
28  * $Id: hci_bcsp.c,v 1.2 2002/09/26 05:05:14 maxk Exp $
29  */
30
31 #define VERSION "0.1"
32
33 #include <linux/config.h>
34 #include <linux/module.h>
35
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/sched.h>
39 #include <linux/types.h>
40 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
42 #include <linux/ptrace.h>
43 #include <linux/poll.h>
44
45 #include <linux/slab.h>
46 #include <linux/tty.h>
47 #include <linux/errno.h>
48 #include <linux/string.h>
49 #include <linux/signal.h>
50 #include <linux/ioctl.h>
51 #include <linux/skbuff.h>
52
53 #include <net/bluetooth/bluetooth.h>
54 #include <net/bluetooth/hci_core.h>
55 #include "hci_uart.h"
56 #include "hci_bcsp.h"
57
58 #ifndef CONFIG_BT_HCIUART_DEBUG
59 #undef  BT_DBG
60 #define BT_DBG( A... )
61 #undef  BT_DMP
62 #define BT_DMP( A... )
63 #endif
64
65 /* ---- BCSP CRC calculation ---- */
66
67 /* Table for calculating CRC for polynomial 0x1021, LSB processed first,
68 initial value 0xffff, bits shifted in reverse order. */
69
70 static const u16 crc_table[] = {
71         0x0000, 0x1081, 0x2102, 0x3183,
72         0x4204, 0x5285, 0x6306, 0x7387,
73         0x8408, 0x9489, 0xa50a, 0xb58b,
74         0xc60c, 0xd68d, 0xe70e, 0xf78f
75 };
76
77 /* Initialise the crc calculator */
78 #define BCSP_CRC_INIT(x) x = 0xffff
79
80 /*
81    Update crc with next data byte
82
83    Implementation note
84         The data byte is treated as two nibbles.  The crc is generated
85         in reverse, i.e., bits are fed into the register from the top.
86 */
87 static void bcsp_crc_update(u16 *crc, u8 d)
88 {
89         u16 reg = *crc;
90
91         reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
92         reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
93
94         *crc = reg;
95 }
96
97 /*
98    Get reverse of generated crc
99
100    Implementation note
101         The crc generator (bcsp_crc_init() and bcsp_crc_update())
102         creates a reversed crc, so it needs to be swapped back before
103         being passed on.
104 */
105 static u16 bcsp_crc_reverse(u16 crc)
106 {
107         u16 b, rev;
108
109         for (b = 0, rev = 0; b < 16; b++) {
110                 rev = rev << 1;
111                 rev |= (crc & 1);
112                 crc = crc >> 1;
113         }
114         return (rev);
115 }
116
117 /* ---- BCSP core ---- */
118
119 static void bcsp_slip_msgdelim(struct sk_buff *skb)
120 {
121         const char pkt_delim = 0xc0;
122         memcpy(skb_put(skb, 1), &pkt_delim, 1);
123 }
124
125 static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c)
126 {
127         const char esc_c0[2] = { 0xdb, 0xdc };
128         const char esc_db[2] = { 0xdb, 0xdd };
129
130         switch (c) {
131         case 0xc0:
132                 memcpy(skb_put(skb, 2), &esc_c0, 2);
133                 break;
134         case 0xdb:
135                 memcpy(skb_put(skb, 2), &esc_db, 2);
136                 break;
137         default:
138                 memcpy(skb_put(skb, 1), &c, 1);
139         }
140 }
141
142 static int bcsp_enqueue(struct hci_uart *hu, struct sk_buff *skb)
143 {
144         struct bcsp_struct *bcsp = hu->priv;
145
146         if (skb->len > 0xFFF) {
147                 BT_ERR("Packet too long");
148                 kfree_skb(skb);
149                 return 0;
150         }
151
152         switch (skb->pkt_type) {
153         case HCI_ACLDATA_PKT:
154         case HCI_COMMAND_PKT:
155                 skb_queue_tail(&bcsp->rel, skb);
156                 break;
157
158         case HCI_SCODATA_PKT:
159                 skb_queue_tail(&bcsp->unrel, skb);
160                 break;
161                 
162         default:
163                 BT_ERR("Unknown packet type");
164                 kfree_skb(skb);
165                 break;
166         }
167         return 0;
168 }
169
170 static struct sk_buff *bcsp_prepare_pkt(struct bcsp_struct *bcsp, u8 *data,
171                 int len, int pkt_type)
172 {
173         struct sk_buff *nskb;
174         u8  hdr[4], chan;
175         int rel, i;
176
177 #ifdef CONFIG_BT_HCIUART_BCSP_TXCRC
178         u16 BCSP_CRC_INIT(bcsp_txmsg_crc);
179 #endif
180
181         switch (pkt_type) {
182         case HCI_ACLDATA_PKT:
183                 chan = 6;       /* BCSP ACL channel */
184                 rel = 1;        /* reliable channel */
185                 break;
186         case HCI_COMMAND_PKT:
187                 chan = 5;       /* BCSP cmd/evt channel */
188                 rel = 1;        /* reliable channel */
189                 break;
190         case HCI_SCODATA_PKT:
191                 chan = 7;       /* BCSP SCO channel */
192                 rel = 0;        /* unreliable channel */
193                 break;
194         case BCSP_LE_PKT:
195                 chan = 1;       /* BCSP LE channel */
196                 rel = 0;        /* unreliable channel */
197                 break;
198         case BCSP_ACK_PKT:
199                 chan = 0;       /* BCSP internal channel */
200                 rel = 0;        /* unreliable channel */
201                 break;
202         default:
203                 BT_ERR("Unknown packet type");
204                 return NULL;
205         }
206
207         /* Max len of packet: (original len +4(bcsp hdr) +2(crc))*2
208            (because bytes 0xc0 and 0xdb are escaped, worst case is
209            when the packet is all made of 0xc0 and 0xdb :) )
210            + 2 (0xc0 delimiters at start and end). */
211
212         nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
213         if (!nskb)
214                 return NULL;
215
216         nskb->pkt_type = pkt_type;
217
218         bcsp_slip_msgdelim(nskb);
219
220         hdr[0] = bcsp->rxseq_txack << 3;
221         bcsp->txack_req = 0;
222         BT_DBG("We request packet no %u to card", bcsp->rxseq_txack);
223
224         if (rel) {
225                 hdr[0] |= 0x80 + bcsp->msgq_txseq;
226                 BT_DBG("Sending packet with seqno %u", bcsp->msgq_txseq);
227                 bcsp->msgq_txseq = ++(bcsp->msgq_txseq) & 0x07;
228         }
229 #ifdef  CONFIG_BT_HCIUART_BCSP_TXCRC
230         hdr[0] |= 0x40;
231 #endif
232
233         hdr[1]  = (len << 4) & 0xFF;
234         hdr[1] |= chan;
235         hdr[2]  = len >> 4;
236         hdr[3]  = ~(hdr[0] + hdr[1] + hdr[2]);
237
238         /* Put BCSP header */
239         for (i = 0; i < 4; i++) {
240                 bcsp_slip_one_byte(nskb, hdr[i]);
241 #ifdef  CONFIG_BT_HCIUART_BCSP_TXCRC
242                 bcsp_crc_update(&bcsp_txmsg_crc, hdr[i]);
243 #endif
244         }
245
246         /* Put payload */
247         for (i = 0; i < len; i++) {
248                 bcsp_slip_one_byte(nskb, data[i]);
249 #ifdef  CONFIG_BT_HCIUART_BCSP_TXCRC
250                 bcsp_crc_update(&bcsp_txmsg_crc, data[i]);
251 #endif
252         }
253
254 #ifdef CONFIG_BT_HCIUART_BCSP_TXCRC
255         /* Put CRC */
256         bcsp_txmsg_crc = bcsp_crc_reverse(bcsp_txmsg_crc);
257         bcsp_slip_one_byte(nskb, (u8) ((bcsp_txmsg_crc >> 8) & 0x00ff));
258         bcsp_slip_one_byte(nskb, (u8) (bcsp_txmsg_crc & 0x00ff));
259 #endif
260
261         bcsp_slip_msgdelim(nskb);
262         return nskb;
263 }
264
265 /* This is a rewrite of pkt_avail in ABCSP */
266 static struct sk_buff *bcsp_dequeue(struct hci_uart *hu)
267 {
268         struct bcsp_struct *bcsp = (struct bcsp_struct *) hu->priv;
269         unsigned long flags;
270         struct sk_buff *skb;
271         
272         /* First of all, check for unreliable messages in the queue,
273            since they have priority */
274
275         if ((skb = skb_dequeue(&bcsp->unrel)) != NULL) {
276                 struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len, skb->pkt_type);
277                 if (nskb) {
278                         kfree_skb(skb);
279                         return nskb;
280                 } else {
281                         skb_queue_head(&bcsp->unrel, skb);
282                         BT_ERR("Could not dequeue pkt because alloc_skb failed");
283                 }
284         }
285
286         /* Now, try to send a reliable pkt. We can only send a
287            reliable packet if the number of packets sent but not yet ack'ed
288            is < than the winsize */
289
290         spin_lock_irqsave(&bcsp->unack.lock, flags);
291
292         if (bcsp->unack.qlen < BCSP_TXWINSIZE && (skb = skb_dequeue(&bcsp->rel)) != NULL) {
293                 struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len, skb->pkt_type);
294                 if (nskb) {
295                         __skb_queue_tail(&bcsp->unack, skb);
296                         mod_timer(&bcsp->tbcsp, jiffies + HZ / 4);
297                         spin_unlock_irqrestore(&bcsp->unack.lock, flags);
298                         return nskb;
299                 } else {
300                         skb_queue_head(&bcsp->rel, skb);
301                         BT_ERR("Could not dequeue pkt because alloc_skb failed");
302                 }
303         }
304
305         spin_unlock_irqrestore(&bcsp->unack.lock, flags);
306
307
308         /* We could not send a reliable packet, either because there are
309            none or because there are too many unack'ed pkts. Did we receive
310            any packets we have not acknowledged yet ? */
311
312         if (bcsp->txack_req) {
313                 /* if so, craft an empty ACK pkt and send it on BCSP unreliable
314                    channel 0 */
315                 struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, NULL, 0, BCSP_ACK_PKT);
316                 return nskb;
317         }
318
319         /* We have nothing to send */
320         return NULL;
321 }
322
323 static int bcsp_flush(struct hci_uart *hu)
324 {
325         BT_DBG("hu %p", hu);
326         return 0;
327 }
328
329 /* Remove ack'ed packets */
330 static void bcsp_pkt_cull(struct bcsp_struct *bcsp)
331 {
332         unsigned long flags;
333         struct sk_buff *skb;
334         int i, pkts_to_be_removed;
335         u8 seqno;
336
337         spin_lock_irqsave(&bcsp->unack.lock, flags);
338
339         pkts_to_be_removed = bcsp->unack.qlen;
340         seqno = bcsp->msgq_txseq;
341
342         while (pkts_to_be_removed) {
343                 if (bcsp->rxack == seqno)
344                         break;
345                 pkts_to_be_removed--;
346                 seqno = (seqno - 1) & 0x07;
347         }
348
349         if (bcsp->rxack != seqno)
350                 BT_ERR("Peer acked invalid packet");
351
352         BT_DBG("Removing %u pkts out of %u, up to seqno %u",
353                pkts_to_be_removed, bcsp->unack.qlen, (seqno - 1) & 0x07);
354
355         for (i = 0, skb = ((struct sk_buff *) &bcsp->unack)->next; i < pkts_to_be_removed
356                         && skb != (struct sk_buff *) &bcsp->unack; i++) {
357                 struct sk_buff *nskb;
358
359                 nskb = skb->next;
360                 __skb_unlink(skb, &bcsp->unack);
361                 kfree_skb(skb);
362                 skb = nskb;
363         }
364         if (bcsp->unack.qlen == 0)
365                 del_timer(&bcsp->tbcsp);
366         spin_unlock_irqrestore(&bcsp->unack.lock, flags);
367
368         if (i != pkts_to_be_removed)
369                 BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed);
370 }
371
372 /* Handle BCSP link-establishment packets. When we
373    detect a "sync" packet, symptom that the BT module has reset,
374    we do nothing :) (yet) */
375 static void bcsp_handle_le_pkt(struct hci_uart *hu)
376 {
377         struct bcsp_struct *bcsp = hu->priv;
378         u8 conf_pkt[4]     = { 0xad, 0xef, 0xac, 0xed };
379         u8 conf_rsp_pkt[4] = { 0xde, 0xad, 0xd0, 0xd0 };
380         u8 sync_pkt[4]     = { 0xda, 0xdc, 0xed, 0xed };
381
382         /* spot "conf" pkts and reply with a "conf rsp" pkt */
383         if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
384                         !memcmp(&bcsp->rx_skb->data[4], conf_pkt, 4)) {
385                 struct sk_buff *nskb = alloc_skb(4, GFP_ATOMIC);
386
387                 BT_DBG("Found a LE conf pkt");
388                 if (!nskb)
389                         return;
390                 memcpy(skb_put(nskb, 4), conf_rsp_pkt, 4);
391                 nskb->pkt_type = BCSP_LE_PKT;
392
393                 skb_queue_head(&bcsp->unrel, nskb);
394                 hci_uart_tx_wakeup(hu);
395         }
396         /* Spot "sync" pkts. If we find one...disaster! */
397         else if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
398                         !memcmp(&bcsp->rx_skb->data[4], sync_pkt, 4)) {
399                 BT_ERR("Found a LE sync pkt, card has reset");
400         }
401 }
402
403 static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char byte)
404 {
405         const u8 c0 = 0xc0, db = 0xdb;
406
407         switch (bcsp->rx_esc_state) {
408         case BCSP_ESCSTATE_NOESC:
409                 switch (byte) {
410                 case 0xdb:
411                         bcsp->rx_esc_state = BCSP_ESCSTATE_ESC;
412                         break;
413                 default:
414                         memcpy(skb_put(bcsp->rx_skb, 1), &byte, 1);
415                         if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
416                                         bcsp->rx_state != BCSP_W4_CRC)
417                                 bcsp_crc_update(&bcsp->message_crc, byte);
418                         bcsp->rx_count--;
419                 }
420                 break;
421
422         case BCSP_ESCSTATE_ESC:
423                 switch (byte) {
424                 case 0xdc:
425                         memcpy(skb_put(bcsp->rx_skb, 1), &c0, 1);
426                         if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
427                                         bcsp->rx_state != BCSP_W4_CRC)
428                                 bcsp_crc_update(&bcsp-> message_crc, 0xc0);
429                         bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
430                         bcsp->rx_count--;
431                         break;
432
433                 case 0xdd:
434                         memcpy(skb_put(bcsp->rx_skb, 1), &db, 1);
435                         if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
436                                         bcsp->rx_state != BCSP_W4_CRC) 
437                                 bcsp_crc_update(&bcsp-> message_crc, 0xdb);
438                         bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
439                         bcsp->rx_count--;
440                         break;
441
442                 default:
443                         BT_ERR ("Invalid byte %02x after esc byte", byte);
444                         kfree_skb(bcsp->rx_skb);
445                         bcsp->rx_skb = NULL;
446                         bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
447                         bcsp->rx_count = 0;
448                 }
449         }
450 }
451
452 static inline void bcsp_complete_rx_pkt(struct hci_uart *hu)
453 {
454         struct bcsp_struct *bcsp = hu->priv;
455         int pass_up;
456
457         if (bcsp->rx_skb->data[0] & 0x80) {     /* reliable pkt */
458                 BT_DBG("Received seqno %u from card", bcsp->rxseq_txack);
459                 bcsp->rxseq_txack++;
460                 bcsp->rxseq_txack %= 0x8;
461                 bcsp->txack_req    = 1;
462
463                 /* If needed, transmit an ack pkt */
464                 hci_uart_tx_wakeup(hu);
465         }
466
467         bcsp->rxack = (bcsp->rx_skb->data[0] >> 3) & 0x07;
468         BT_DBG("Request for pkt %u from card", bcsp->rxack);
469
470         bcsp_pkt_cull(bcsp);
471         if ((bcsp->rx_skb->data[1] & 0x0f) == 6 &&
472                         bcsp->rx_skb->data[0] & 0x80) {
473                 bcsp->rx_skb->pkt_type = HCI_ACLDATA_PKT;
474                 pass_up = 1;
475         } else if ((bcsp->rx_skb->data[1] & 0x0f) == 5 &&
476                         bcsp->rx_skb->data[0] & 0x80) {
477                 bcsp->rx_skb->pkt_type = HCI_EVENT_PKT;
478                 pass_up = 1;
479         } else if ((bcsp->rx_skb->data[1] & 0x0f) == 7) {
480                 bcsp->rx_skb->pkt_type = HCI_SCODATA_PKT;
481                 pass_up = 1;
482         } else if ((bcsp->rx_skb->data[1] & 0x0f) == 1 &&
483                         !(bcsp->rx_skb->data[0] & 0x80)) {
484                 bcsp_handle_le_pkt(hu);
485                 pass_up = 0;
486         } else
487                 pass_up = 0;
488
489         if (!pass_up) {
490                 if ((bcsp->rx_skb->data[1] & 0x0f) != 0 &&
491                         (bcsp->rx_skb->data[1] & 0x0f) != 1) {
492                         BT_ERR ("Packet for unknown channel (%u %s)",
493                                 bcsp->rx_skb->data[1] & 0x0f,
494                                 bcsp->rx_skb->data[0] & 0x80 ? 
495                                 "reliable" : "unreliable");
496                 }
497                 kfree_skb(bcsp->rx_skb);
498         } else {
499                 /* Pull out BCSP hdr */
500                 skb_pull(bcsp->rx_skb, 4);
501
502                 hci_recv_frame(bcsp->rx_skb);
503         }
504         bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
505         bcsp->rx_skb = NULL;
506 }
507
508 /* Recv data */
509 static int bcsp_recv(struct hci_uart *hu, void *data, int count)
510 {
511         struct bcsp_struct *bcsp = hu->priv;
512         register unsigned char *ptr;
513
514         BT_DBG("hu %p count %d rx_state %d rx_count %ld", 
515                 hu, count, bcsp->rx_state, bcsp->rx_count);
516
517         ptr = data;
518         while (count) {
519                 if (bcsp->rx_count) {
520                         if (*ptr == 0xc0) {
521                                 BT_ERR("Short BCSP packet");
522                                 kfree_skb(bcsp->rx_skb);
523                                 bcsp->rx_state = BCSP_W4_PKT_START;
524                                 bcsp->rx_count = 0;
525                         } else
526                                 bcsp_unslip_one_byte(bcsp, *ptr);
527
528                         ptr++; count--;
529                         continue;
530                 }
531
532                 switch (bcsp->rx_state) {
533                 case BCSP_W4_BCSP_HDR:
534                         if ((0xff & (u8) ~ (bcsp->rx_skb->data[0] + bcsp->rx_skb->data[1] +
535                                         bcsp->rx_skb->data[2])) != bcsp->rx_skb->data[3]) {
536                                 BT_ERR("Error in BCSP hdr checksum");
537                                 kfree_skb(bcsp->rx_skb);
538                                 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
539                                 bcsp->rx_count = 0;
540                                 continue;
541                         }
542                         if (bcsp->rx_skb->data[0] & 0x80        /* reliable pkt */
543                                         && (bcsp->rx_skb->data[0] & 0x07) != bcsp->rxseq_txack) {
544                                 BT_ERR ("Out-of-order packet arrived, got %u expected %u",
545                                         bcsp->rx_skb->data[0] & 0x07, bcsp->rxseq_txack);
546
547                                 kfree_skb(bcsp->rx_skb);
548                                 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
549                                 bcsp->rx_count = 0;
550                                 continue;
551                         }
552                         bcsp->rx_state = BCSP_W4_DATA;
553                         bcsp->rx_count = (bcsp->rx_skb->data[1] >> 4) + 
554                                         (bcsp->rx_skb->data[2] << 4);   /* May be 0 */
555                         continue;
556
557                 case BCSP_W4_DATA:
558                         if (bcsp->rx_skb->data[0] & 0x40) {     /* pkt with crc */
559                                 bcsp->rx_state = BCSP_W4_CRC;
560                                 bcsp->rx_count = 2;
561                         } else
562                                 bcsp_complete_rx_pkt(hu);
563                         continue;
564
565                 case BCSP_W4_CRC:
566                         if (bcsp_crc_reverse(bcsp->message_crc) !=
567                                         (bcsp->rx_skb->data[bcsp->rx_skb->len - 2] << 8) +
568                                         bcsp->rx_skb->data[bcsp->rx_skb->len - 1]) {
569
570                                 BT_ERR ("Checksum failed: computed %04x received %04x",
571                                         bcsp_crc_reverse(bcsp->message_crc),
572                                         (bcsp->rx_skb-> data[bcsp->rx_skb->len - 2] << 8) +
573                                         bcsp->rx_skb->data[bcsp->rx_skb->len - 1]);
574
575                                 kfree_skb(bcsp->rx_skb);
576                                 bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
577                                 bcsp->rx_count = 0;
578                                 continue;
579                         }
580                         skb_trim(bcsp->rx_skb, bcsp->rx_skb->len - 2);
581                         bcsp_complete_rx_pkt(hu);
582                         continue;
583
584                 case BCSP_W4_PKT_DELIMITER:
585                         switch (*ptr) {
586                         case 0xc0:
587                                 bcsp->rx_state = BCSP_W4_PKT_START;
588                                 break;
589                         default:
590                                 /*BT_ERR("Ignoring byte %02x", *ptr);*/
591                                 break;
592                         }
593                         ptr++; count--;
594                         break;
595
596                 case BCSP_W4_PKT_START:
597                         switch (*ptr) {
598                         case 0xc0:
599                                 ptr++; count--;
600                                 break;
601
602                         default:
603                                 bcsp->rx_state = BCSP_W4_BCSP_HDR;
604                                 bcsp->rx_count = 4;
605                                 bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
606                                 BCSP_CRC_INIT(bcsp->message_crc);
607                                 
608                                 /* Do not increment ptr or decrement count
609                                  * Allocate packet. Max len of a BCSP pkt= 
610                                  * 0xFFF (payload) +4 (header) +2 (crc) */
611
612                                 bcsp->rx_skb = bt_skb_alloc(0x1005, GFP_ATOMIC);
613                                 if (!bcsp->rx_skb) {
614                                         BT_ERR("Can't allocate mem for new packet");
615                                         bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
616                                         bcsp->rx_count = 0;
617                                         return 0;
618                                 }
619                                 bcsp->rx_skb->dev = (void *) hu->hdev;
620                                 break;
621                         }
622                         break;
623                 }
624         }
625         return count;
626 }
627
628         /* Arrange to retransmit all messages in the relq. */
629 static void bcsp_timed_event(unsigned long arg)
630 {
631         struct hci_uart *hu = (struct hci_uart *) arg;
632         struct bcsp_struct *bcsp = (struct bcsp_struct *) hu->priv;
633         struct sk_buff *skb;
634         unsigned long flags;
635
636         BT_ERR("Timeout, retransmitting %u pkts", bcsp->unack.qlen);
637         spin_lock_irqsave(&bcsp->unack.lock, flags);
638
639         while ((skb = __skb_dequeue_tail(&bcsp->unack)) != NULL) {
640                 bcsp->msgq_txseq = (bcsp->msgq_txseq - 1) & 0x07;
641                 skb_queue_head(&bcsp->rel, skb);
642         }
643
644         spin_unlock_irqrestore(&bcsp->unack.lock, flags);
645
646         hci_uart_tx_wakeup(hu);
647 }
648
649 static int bcsp_open(struct hci_uart *hu)
650 {
651         struct bcsp_struct *bcsp;
652
653         BT_DBG("hu %p", hu);
654
655         bcsp = kmalloc(sizeof(*bcsp), GFP_ATOMIC);
656         if (!bcsp)
657                 return -ENOMEM;
658         memset(bcsp, 0, sizeof(*bcsp));
659
660         hu->priv = bcsp;
661         skb_queue_head_init(&bcsp->unack);
662         skb_queue_head_init(&bcsp->rel);
663         skb_queue_head_init(&bcsp->unrel);
664
665         init_timer(&bcsp->tbcsp);
666         bcsp->tbcsp.function = bcsp_timed_event;
667         bcsp->tbcsp.data     = (u_long) hu;
668
669         bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
670
671         return 0;
672 }
673
674 static int bcsp_close(struct hci_uart *hu)
675 {
676         struct bcsp_struct *bcsp = hu->priv;
677         hu->priv = NULL;
678
679         BT_DBG("hu %p", hu);
680
681         skb_queue_purge(&bcsp->unack);
682         skb_queue_purge(&bcsp->rel);
683         skb_queue_purge(&bcsp->unrel);
684         del_timer(&bcsp->tbcsp);
685
686         kfree(bcsp);
687         return 0;
688 }
689
690 static struct hci_uart_proto bcsp = {
691         .id      = HCI_UART_BCSP,
692         .open    = bcsp_open,
693         .close   = bcsp_close,
694         .enqueue = bcsp_enqueue,
695         .dequeue = bcsp_dequeue,
696         .recv    = bcsp_recv,
697         .flush   = bcsp_flush
698 };
699
700 int bcsp_init(void)
701 {
702         int err = hci_uart_register_proto(&bcsp);
703         if (!err)
704                 BT_INFO("HCI BCSP protocol initialized");
705         else
706                 BT_ERR("HCI BCSP protocol registration failed");
707
708         return err;
709 }
710
711 int bcsp_deinit(void)
712 {
713         return hci_uart_unregister_proto(&bcsp);
714 }