Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android+android...
[firefly-linux-kernel-4.4.55.git] / drivers / bluetooth / hci_rtk_h5.c
1 /*
2  *
3  *  Bluetooth HCI UART driver
4  *
5  *  Copyright (C) 2011-2014  wifi_fae<wifi_fae@realtek.com.tw>
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/module.h>
25
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/fcntl.h>
30 #include <linux/interrupt.h>
31 #include <linux/ptrace.h>
32 #include <linux/poll.h>
33
34 #include <linux/slab.h>
35 #include <linux/tty.h>
36 #include <linux/errno.h>
37 #include <linux/string.h>
38 #include <linux/signal.h>
39 #include <linux/ioctl.h>
40 #include <linux/skbuff.h>
41 #include <linux/bitrev.h>
42 #include <asm/unaligned.h>
43
44 #include <net/bluetooth/bluetooth.h>
45 #include <net/bluetooth/hci_core.h>
46
47 #include "hci_uart.h"
48
49 #define VERSION "1.0"
50
51 static int txcrc = 1;
52 //static int hciextn = 1;
53
54 #define H5_TXWINSIZE    4
55 #define H5_ACK_PKT      0x00
56 #define H5_LE_PKT           0x0F
57 #define H5_VDRSPEC_PKT  0x0E
58
59 struct h5_struct {
60         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 */
63
64         unsigned long rx_count;
65         struct  sk_buff *rx_skb;
66         u8      rxseq_txack;            /* rxseq == txack. */
67         u8      rxack;                  /* Last packet sent by us that the peer ack'ed */
68         struct  timer_list th5;
69
70         enum {
71                 H5_W4_PKT_DELIMITER,
72                 H5_W4_PKT_START,
73                 H5_W4_HDR,
74                 H5_W4_DATA,
75                 H5_W4_CRC
76         } rx_state;
77
78         enum {
79                 H5_ESCSTATE_NOESC,
80                 H5_ESCSTATE_ESC
81         } rx_esc_state;
82
83         u8      use_crc;
84         u16     message_crc;
85         u8      txack_req;              /* Do we need to send ack's to the peer? */
86
87         /* Reliable packet sequence number - used to assign seq to each rel pkt. */
88         u8      msgq_txseq;
89 };
90
91 /* ---- H5 CRC calculation ---- */
92
93 /* Table for calculating CRC for polynomial 0x1021, LSB processed first,
94 initial value 0xffff, bits shifted in reverse order. */
95
96 static const u16 crc_table[] = {
97         0x0000, 0x1081, 0x2102, 0x3183,
98         0x4204, 0x5285, 0x6306, 0x7387,
99         0x8408, 0x9489, 0xa50a, 0xb58b,
100         0xc60c, 0xd68d, 0xe70e, 0xf78f
101 };
102
103 /* Initialise the crc calculator */
104 #define H5_CRC_INIT(x) x = 0xffff
105
106 /*
107    Update crc with next data byte
108
109    Implementation note
110         The data byte is treated as two nibbles.  The crc is generated
111         in reverse, i.e., bits are fed into the register from the top.
112 */
113 static void h5_crc_update(u16 *crc, u8 d)
114 {
115         u16 reg = *crc;
116
117         reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
118         reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
119
120         *crc = reg;
121 }
122
123 /* ---- H5 core ---- */
124
125 static void h5_slip_msgdelim(struct sk_buff *skb)
126 {
127         const char pkt_delim = 0xc0;
128
129         memcpy(skb_put(skb, 1), &pkt_delim, 1);
130 }
131
132 static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
133 {
134         const char esc_c0[2] = { 0xdb, 0xdc };
135         const char esc_db[2] = { 0xdb, 0xdd };
136         const char esc_11[2] = { 0xdb, 0xde };
137         const char esc_13[2] = { 0xdb, 0xdf };
138         
139         switch (c) {
140         case 0xc0:
141                 memcpy(skb_put(skb, 2), &esc_c0, 2);
142                 break;
143         case 0xdb:
144                 memcpy(skb_put(skb, 2), &esc_db, 2);
145                 break;
146         case 0x11:
147                 memcpy(skb_put(skb, 2), &esc_11, 2);
148                 break;
149         case 0x13:
150                 memcpy(skb_put(skb, 2), &esc_13, 2);
151                 break;
152         default:
153                 memcpy(skb_put(skb, 1), &c, 1);
154         }
155 }
156
157 static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
158 {
159         struct h5_struct *h5 = hu->priv;
160
161         if (skb->len > 0xFFF) { //Pkt length must be less than 4095 bytes    
162                 BT_ERR("Packet too long");
163                 kfree_skb(skb);
164                 return 0;
165         }
166
167         switch (bt_cb(skb)->pkt_type) {
168         case HCI_ACLDATA_PKT:
169         case HCI_COMMAND_PKT:
170             skb_queue_tail(&h5->rel, skb);
171             break;
172
173         case HCI_SCODATA_PKT:
174             skb_queue_tail(&h5->unrel, skb);
175             break;
176         case H5_LE_PKT:
177         case H5_ACK_PKT:
178         case H5_VDRSPEC_PKT:
179             skb_queue_tail(&h5->unrel, skb);    /* 3-wire LinkEstablishment*/
180             break;
181                 
182         default:
183             BT_ERR("Unknown packet type");
184             kfree_skb(skb);
185             break;
186         }
187
188         return 0;
189 }
190
191 static struct sk_buff *h5_prepare_pkt(struct h5_struct *h5, u8 *data,
192                 int len, int pkt_type)
193 {
194         struct sk_buff *nskb;
195         u8 hdr[4], chan;
196         u16 H5_CRC_INIT(h5_txmsg_crc);
197         int rel, i;
198
199         switch (pkt_type) {
200         case HCI_ACLDATA_PKT:
201             chan = 2;   /* 3-wire ACL channel */
202             rel = 1;    /* reliable channel */
203             break;
204         case HCI_COMMAND_PKT:
205             chan = 1;   /* 3-wire cmd channel */
206             rel = 1;    /* reliable channel */
207             break;
208         case HCI_EVENT_PKT:
209             chan = 4;   /* 3-wire cmd channel */
210             rel = 1;    /* reliable channel */
211             break;
212         case HCI_SCODATA_PKT:
213             chan = 3;   /* 3-wire SCO channel */
214             rel = 0;    /* unreliable channel */   
215             break;
216         case H5_LE_PKT:
217             chan = 15;  /* 3-wire LinkEstablishment channel */
218             rel = 0;    /* unreliable channel */
219             break;
220         case H5_ACK_PKT:
221             chan = 0;   /* 3-wire ACK channel */
222             rel = 0;    /* unreliable channel */
223             break;
224         case H5_VDRSPEC_PKT:
225             chan = 14;  /* 3-wire Vendor Specific channel */
226             rel = 0;    /* unreliable channel */
227             break;
228         default:
229             BT_ERR("Unknown packet type");
230             return NULL;
231         }
232
233
234         /* Max len of packet: (original len +4(h5 hdr) +2(crc))*2
235            (because bytes 0xc0 and 0xdb are escaped, worst case is
236            when the packet is all made of 0xc0 and 0xdb :) )
237            + 2 (0xc0 delimiters at start and end). */
238
239         nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
240         if (!nskb)
241                 return NULL;
242
243         bt_cb(nskb)->pkt_type = pkt_type;
244
245         h5_slip_msgdelim(nskb);
246
247         hdr[0] = h5->rxseq_txack << 3;
248         h5->txack_req = 0;
249         BT_DBG("We request packet no %u to card", h5->rxseq_txack);
250
251         if (rel) {
252                 hdr[0] |= 0x80 + h5->msgq_txseq;
253                 BT_DBG("Sending packet with seqno %u", h5->msgq_txseq);
254                 h5->msgq_txseq = (h5->msgq_txseq + 1) & 0x07;
255         }
256
257         if (h5->use_crc)
258                 hdr[0] |= 0x40;
259
260         hdr[1] = ((len << 4) & 0xff) | chan;
261         hdr[2] = len >> 4;
262         hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);
263
264         /* Put H5 header */
265         for (i = 0; i < 4; i++) {
266                 h5_slip_one_byte(nskb, hdr[i]);
267
268                 if (h5->use_crc)
269                         h5_crc_update(&h5_txmsg_crc, hdr[i]);
270         }
271
272         /* Put payload */
273         for (i = 0; i < len; i++) {
274                 h5_slip_one_byte(nskb, data[i]);
275
276                 if (h5->use_crc)
277                         h5_crc_update(&h5_txmsg_crc, data[i]);
278         }
279
280         /* Put CRC */
281         if (h5->use_crc) {
282                 h5_txmsg_crc = bitrev16(h5_txmsg_crc);
283                 h5_slip_one_byte(nskb, (u8) ((h5_txmsg_crc >> 8) & 0x00ff));
284                 h5_slip_one_byte(nskb, (u8) (h5_txmsg_crc & 0x00ff));
285         }
286
287         h5_slip_msgdelim(nskb);
288         return nskb;
289 }
290
291 /* This is a rewrite of pkt_avail in AH5 */
292 static struct sk_buff *h5_dequeue(struct hci_uart *hu)
293 {
294         struct h5_struct *h5 = hu->priv;
295         unsigned long flags;
296         struct sk_buff *skb;
297         
298         /* First of all, check for unreliable messages in the queue,
299            since they have priority */
300
301         if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
302                 struct sk_buff *nskb = h5_prepare_pkt(h5, skb->data, skb->len, bt_cb(skb)->pkt_type);
303                 if (nskb) {
304                         kfree_skb(skb);
305                         return nskb;
306                 } else {
307                         skb_queue_head(&h5->unrel, skb);
308                         BT_ERR("Could not dequeue pkt because alloc_skb failed");
309                 }
310         }
311
312         /* Now, try to send a reliable pkt. We can only send a
313            reliable packet if the number of packets sent but not yet ack'ed
314            is < than the winsize */
315
316         spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
317
318         if (h5->unack.qlen < H5_TXWINSIZE && (skb = skb_dequeue(&h5->rel)) != NULL) {
319                 struct sk_buff *nskb = h5_prepare_pkt(h5, skb->data, skb->len, bt_cb(skb)->pkt_type);
320                 if (nskb) {
321                         __skb_queue_tail(&h5->unack, skb);
322                         mod_timer(&h5->th5, jiffies + HZ / 4);
323                         spin_unlock_irqrestore(&h5->unack.lock, flags);
324                         return nskb;
325                 } else {
326                         skb_queue_head(&h5->rel, skb);
327                         BT_ERR("Could not dequeue pkt because alloc_skb failed");
328                 }
329         }
330
331         spin_unlock_irqrestore(&h5->unack.lock, flags);
332
333         /* We could not send a reliable packet, either because there are
334            none or because there are too many unack'ed pkts. Did we receive
335            any packets we have not acknowledged yet ? */
336
337         if (h5->txack_req) {
338                 /* if so, craft an empty ACK pkt and send it on H5 unreliable
339                    channel 0 */
340                 struct sk_buff *nskb = h5_prepare_pkt(h5, NULL, 0, H5_ACK_PKT);
341                 return nskb;
342         }
343
344         /* We have nothing to send */
345         return NULL;
346 }
347
348 static int h5_flush(struct hci_uart *hu)
349 {
350         BT_DBG("hu %p", hu);
351         return 0;
352 }
353
354 /* Remove ack'ed packets */
355 static void h5_pkt_cull(struct h5_struct *h5)
356 {
357         struct sk_buff *skb, *tmp;
358         unsigned long flags;
359         int i, pkts_to_be_removed;
360         u8 seqno;
361
362         spin_lock_irqsave(&h5->unack.lock, flags);
363
364         pkts_to_be_removed = skb_queue_len(&h5->unack);
365         seqno = h5->msgq_txseq;
366
367         while (pkts_to_be_removed) {
368                 if (h5->rxack == seqno)
369                         break;
370                 pkts_to_be_removed--;
371                 seqno = (seqno - 1) & 0x07;
372         }
373
374         if (h5->rxack != seqno)
375                 BT_ERR("Peer acked invalid packet");
376
377         BT_DBG("Removing %u pkts out of %u, up to seqno %u",
378                pkts_to_be_removed, skb_queue_len(&h5->unack),
379                (seqno - 1) & 0x07);
380
381         i = 0;
382         skb_queue_walk_safe(&h5->unack, skb, tmp) {
383                 if (i >= pkts_to_be_removed)
384                         break;
385                 i++;
386
387                 __skb_unlink(skb, &h5->unack);
388                 kfree_skb(skb);
389         }
390
391         if (skb_queue_empty(&h5->unack))
392                 del_timer(&h5->th5);
393
394         spin_unlock_irqrestore(&h5->unack.lock, flags);
395
396         if (i != pkts_to_be_removed)
397                 BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed);
398 }
399
400 /* Handle H5 link-establishment packets. When we
401    detect a "sync" packet, symptom that the BT module has reset,
402    we do nothing :) (yet) */
403 static void h5_handle_le_pkt(struct hci_uart *hu)
404 {
405         struct h5_struct *h5 = hu->priv;
406         u8 conf_pkt[2]     = { 0x03, 0xfc};
407         u8 conf_rsp_pkt[3] = { 0x04, 0x7b, 0x00};
408         u8 sync_pkt[2]     = { 0x01, 0x7e};
409         u8 sync_rsp_pkt[2] = { 0x02, 0x7d};
410
411         u8 wakeup_pkt[2]   = { 0x05, 0xfa};
412         u8 woken_pkt[2]    = { 0x06, 0xf9};
413         u8 sleep_pkt[2]    = { 0x07, 0x78};
414
415         /* spot "conf" pkts and reply with a "conf rsp" pkt */
416         if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
417                         !memcmp(&h5->rx_skb->data[4], conf_pkt, 2)) {
418                 struct sk_buff *nskb = alloc_skb(3, GFP_ATOMIC);
419
420                 BT_DBG("Found a LE conf pkt");
421                 if (!nskb)
422                         return;
423
424                 conf_rsp_pkt[2] |= txcrc << 0x4; //crc check enable, version no = 0. needed to be as avariable.
425                 memcpy(skb_put(nskb, 3), conf_rsp_pkt, 3);
426                 bt_cb(nskb)->pkt_type = H5_LE_PKT;
427
428                 skb_queue_head(&h5->unrel, nskb);
429                 hci_uart_tx_wakeup(hu);
430         }
431         /* spot "conf resp" pkts*/
432         else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
433                         !memcmp(&h5->rx_skb->data[4], conf_rsp_pkt, 2)) {
434                 BT_DBG("Found a LE conf resp pkt, device go into active state");
435                 txcrc = (h5->rx_skb->data[6] >> 0x4) & 0x1;
436         }
437
438         /* Spot "sync" pkts. If we find one...disaster! */
439         else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
440                         !memcmp(&h5->rx_skb->data[4], sync_pkt, 2)) {
441                 BT_ERR("Found a LE sync pkt, card has reset");
442                 //DO Something here
443         }
444         /* Spot "sync resp" pkts. If we find one...disaster! */
445         else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
446                         !memcmp(&h5->rx_skb->data[4], sync_rsp_pkt, 2)) {
447                 BT_ERR("Found a LE sync resp pkt, device go into initialized state");
448                 //      DO Something here
449         }
450         /* Spot "wakeup" pkts. reply woken message when in active mode */
451         else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
452                         !memcmp(&h5->rx_skb->data[4], wakeup_pkt, 2)) {
453                 struct sk_buff *nskb = alloc_skb(2, GFP_ATOMIC);
454
455                 BT_ERR("Found a LE Wakeup pkt, and reply woken message");
456                 //      DO Something here
457
458                 memcpy(skb_put(nskb, 2), woken_pkt, 2);
459                 bt_cb(nskb)->pkt_type = H5_LE_PKT;
460
461                 skb_queue_head(&h5->unrel, nskb);
462                 hci_uart_tx_wakeup(hu);
463         }
464         /* Spot "woken" pkts. receive woken message from device */
465         else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
466                         !memcmp(&h5->rx_skb->data[4], woken_pkt, 2)) {
467                 BT_ERR("Found a LE woken pkt from device");
468                 //      DO Something here
469         }
470         /* Spot "Sleep" pkts*/
471         else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
472                         !memcmp(&h5->rx_skb->data[4], sleep_pkt, 2)) {
473                 BT_ERR("Found a LE Sleep pkt");
474                 //      DO Something here
475         }
476
477 }
478
479 static inline void h5_unslip_one_byte(struct h5_struct *h5, unsigned char byte)
480 {
481         const u8 c0   = 0xc0, db   = 0xdb;
482         const u8 oof1 = 0x11, oof2 = 0x13;
483
484         switch (h5->rx_esc_state) {
485         case H5_ESCSTATE_NOESC:
486                 switch (byte) {
487                 case 0xdb:
488                         h5->rx_esc_state = H5_ESCSTATE_ESC;
489                         break;
490                 default:
491                         memcpy(skb_put(h5->rx_skb, 1), &byte, 1);
492                         if ((h5->rx_skb-> data[0] & 0x40) != 0 && 
493                                         h5->rx_state != H5_W4_CRC)
494                                 h5_crc_update(&h5->message_crc, byte);
495                         h5->rx_count--;
496                 }
497                 break;
498
499         case H5_ESCSTATE_ESC:
500                 switch (byte) {
501                 case 0xdc:
502                         memcpy(skb_put(h5->rx_skb, 1), &c0, 1);
503                         if ((h5->rx_skb-> data[0] & 0x40) != 0 && 
504                                         h5->rx_state != H5_W4_CRC)
505                                 h5_crc_update(&h5-> message_crc, 0xc0);
506                         h5->rx_esc_state = H5_ESCSTATE_NOESC;
507                         h5->rx_count--;
508                         break;
509
510                 case 0xdd:
511                         memcpy(skb_put(h5->rx_skb, 1), &db, 1);
512                         if ((h5->rx_skb-> data[0] & 0x40) != 0 && 
513                                         h5->rx_state != H5_W4_CRC) 
514                                 h5_crc_update(&h5-> message_crc, 0xdb);
515                         h5->rx_esc_state = H5_ESCSTATE_NOESC;
516                         h5->rx_count--;
517                         break;
518                         
519                 case 0xde:
520                         memcpy(skb_put(h5->rx_skb, 1), &oof1, 1);
521                         if ((h5->rx_skb-> data[0] & 0x40) != 0 && h5->rx_state != H5_W4_CRC)
522                                 h5_crc_update(&h5-> message_crc, oof1);
523                         h5->rx_esc_state = H5_ESCSTATE_NOESC;
524                         h5->rx_count--;
525                         break;
526
527                 case 0xdf:
528                         memcpy(skb_put(h5->rx_skb, 1), &oof2, 1);
529                         if ((h5->rx_skb-> data[0] & 0x40) != 0 && h5->rx_state != H5_W4_CRC) 
530                                 h5_crc_update(&h5-> message_crc, oof2);
531                         h5->rx_esc_state = H5_ESCSTATE_NOESC;
532                         h5->rx_count--;
533                         break;
534
535                 default:
536                         BT_ERR ("Invalid byte %02x after esc byte", byte);
537                         kfree_skb(h5->rx_skb);
538                         h5->rx_skb = NULL;
539                         h5->rx_state = H5_W4_PKT_DELIMITER;
540                         h5->rx_count = 0;
541                 }
542         }
543 }
544
545 static void h5_complete_rx_pkt(struct hci_uart *hu)
546 {
547         struct h5_struct *h5 = hu->priv;
548         int pass_up;
549
550         if (h5->rx_skb->data[0] & 0x80) {       /* reliable pkt */
551                 BT_DBG("Received seqno %u from card", h5->rxseq_txack);
552                 h5->rxseq_txack++;
553                 h5->rxseq_txack %= 0x8;
554                 h5->txack_req    = 1;
555
556                 /* If needed, transmit an ack pkt */
557                 hci_uart_tx_wakeup(hu);
558         }
559
560         h5->rxack = (h5->rx_skb->data[0] >> 3) & 0x07;
561         BT_DBG("Request for pkt %u from card", h5->rxack);
562
563         h5_pkt_cull(h5);
564
565         if ((h5->rx_skb->data[1] & 0x0f) == 2 && 
566                         h5->rx_skb->data[0] & 0x80) {
567                 bt_cb(h5->rx_skb)->pkt_type = HCI_ACLDATA_PKT;
568                 pass_up = 1;
569         } else if ((h5->rx_skb->data[1] & 0x0f) == 4 && 
570                         h5->rx_skb->data[0] & 0x80) {
571                 bt_cb(h5->rx_skb)->pkt_type = HCI_EVENT_PKT;
572                 pass_up = 1;
573         } else if ((h5->rx_skb->data[1] & 0x0f) == 3) {
574                 bt_cb(h5->rx_skb)->pkt_type = HCI_SCODATA_PKT;
575                 pass_up = 1;
576         } else if ((h5->rx_skb->data[1] & 0x0f) == 15 && 
577                         !(h5->rx_skb->data[0] & 0x80)) {
578                 //h5_handle_le_pkt(hu);//Link Establishment Pkt
579                 pass_up = 0;
580         } else if ((h5->rx_skb->data[1] & 0x0f) == 1 && 
581                         h5->rx_skb->data[0] & 0x80) {
582                 bt_cb(h5->rx_skb)->pkt_type = HCI_COMMAND_PKT;
583                 pass_up = 1;
584         } else if ((h5->rx_skb->data[1] & 0x0f) == 14) {
585                 bt_cb(h5->rx_skb)->pkt_type = H5_VDRSPEC_PKT;
586                 pass_up = 1;
587         } else
588                 pass_up = 0;
589
590         if (!pass_up) {         
591                 struct hci_event_hdr hdr;
592                 u8 desc = (h5->rx_skb->data[1] & 0x0f);
593
594                 if (desc != H5_ACK_PKT && desc != H5_LE_PKT) {
595 #if 0                   
596                         if (hciextn) {
597                                 desc |= 0xc0;
598                                 skb_pull(h5->rx_skb, 4);
599                                 memcpy(skb_push(h5->rx_skb, 1), &desc, 1);
600
601                                 hdr.evt = 0xff;
602                                 hdr.plen = h5->rx_skb->len;
603                                 memcpy(skb_push(h5->rx_skb, HCI_EVENT_HDR_SIZE), &hdr, HCI_EVENT_HDR_SIZE);
604                                 bt_cb(h5->rx_skb)->pkt_type = HCI_EVENT_PKT;
605
606                                 hci_recv_frame(h5->rx_skb);
607                         } else {
608 #endif                          
609                                 BT_ERR ("Packet for unknown channel (%u %s)",
610                                         h5->rx_skb->data[1] & 0x0f,
611                                         h5->rx_skb->data[0] & 0x80 ? 
612                                         "reliable" : "unreliable");
613                                 kfree_skb(h5->rx_skb);
614 //                      }
615                 } else
616                         kfree_skb(h5->rx_skb);
617         } else {
618                 /* Pull out H5 hdr */
619                 skb_pull(h5->rx_skb, 4);
620
621                 hci_recv_frame(h5->rx_skb);
622         }
623
624         h5->rx_state = H5_W4_PKT_DELIMITER;
625         h5->rx_skb = NULL;
626 }
627
628 static u16 bscp_get_crc(struct h5_struct *h5)
629 {
630         return get_unaligned_be16(&h5->rx_skb->data[h5->rx_skb->len - 2]);
631 }
632
633 /* Recv data */
634 static int h5_recv(struct hci_uart *hu, void *data, int count)
635 {
636         struct h5_struct *h5 = hu->priv;
637         register unsigned char *ptr;
638
639         BT_DBG("hu %p count %d rx_state %d rx_count %ld", 
640                 hu, count, h5->rx_state, h5->rx_count);
641
642         ptr = data;
643         while (count) {
644                 if (h5->rx_count) {
645                         if (*ptr == 0xc0) {
646                                 BT_ERR("Short H5 packet");
647                                 kfree_skb(h5->rx_skb);
648                                 h5->rx_state = H5_W4_PKT_START;
649                                 h5->rx_count = 0;
650                         } else
651                                 h5_unslip_one_byte(h5, *ptr);
652
653                         ptr++; count--;
654                         continue;
655                 }
656
657                 switch (h5->rx_state) {
658                 case H5_W4_HDR:
659                         if ((0xff & (u8) ~ (h5->rx_skb->data[0] + h5->rx_skb->data[1] +
660                                         h5->rx_skb->data[2])) != h5->rx_skb->data[3]) {
661                                 BT_ERR("Error in H5 hdr checksum");
662                                 kfree_skb(h5->rx_skb);
663                                 h5->rx_state = H5_W4_PKT_DELIMITER;
664                                 h5->rx_count = 0;
665                                 continue;
666                         }
667                         if (h5->rx_skb->data[0] & 0x80  /* reliable pkt */
668                                         && (h5->rx_skb->data[0] & 0x07) != h5->rxseq_txack) {
669                                 BT_ERR ("Out-of-order packet arrived, got %u expected %u",
670                                         h5->rx_skb->data[0] & 0x07, h5->rxseq_txack);
671
672                                 h5->txack_req = 1;
673                                 hci_uart_tx_wakeup(hu);                 
674                                 kfree_skb(h5->rx_skb);
675                                 h5->rx_state = H5_W4_PKT_DELIMITER;
676                                 h5->rx_count = 0;
677                                 continue;
678                         }
679                         h5->rx_state = H5_W4_DATA;
680                         h5->rx_count = (h5->rx_skb->data[1] >> 4) + 
681                                         (h5->rx_skb->data[2] << 4);     /* May be 0 */
682                         continue;
683
684                 case H5_W4_DATA:
685                         if (h5->rx_skb->data[0] & 0x40) {       /* pkt with crc */
686                                 h5->rx_state = H5_W4_CRC;
687                                 h5->rx_count = 2;
688                         } else
689                                 h5_complete_rx_pkt(hu);
690                         continue;
691
692                 case H5_W4_CRC:
693                         if (bitrev16(h5->message_crc) != bscp_get_crc(h5)) {
694                                 BT_ERR ("Checksum failed: computed %04x received %04x",
695                                         bitrev16(h5->message_crc),
696                                         bscp_get_crc(h5));
697
698                                 kfree_skb(h5->rx_skb);
699                                 h5->rx_state = H5_W4_PKT_DELIMITER;
700                                 h5->rx_count = 0;
701                                 continue;
702                         }
703                         skb_trim(h5->rx_skb, h5->rx_skb->len - 2);
704                         h5_complete_rx_pkt(hu);
705                         continue;
706
707                 case H5_W4_PKT_DELIMITER:
708                         switch (*ptr) {
709                         case 0xc0:
710                                 h5->rx_state = H5_W4_PKT_START;
711                                 break;
712                         default:
713                                 /*BT_ERR("Ignoring byte %02x", *ptr);*/
714                                 break;
715                         }
716                         ptr++; count--;
717                         break;
718
719                 case H5_W4_PKT_START:
720                         switch (*ptr) {
721                         case 0xc0:
722                                 ptr++; count--;
723                                 break;
724
725                         default:
726                                 h5->rx_state = H5_W4_HDR;
727                                 h5->rx_count = 4;
728                                 h5->rx_esc_state = H5_ESCSTATE_NOESC;
729                                 H5_CRC_INIT(h5->message_crc);
730
731                                 /* Do not increment ptr or decrement count
732                                  * Allocate packet. Max len of a H5 pkt= 
733                                  * 0xFFF (payload) +4 (header) +2 (crc) */
734
735                                 h5->rx_skb = bt_skb_alloc(0x1005, GFP_ATOMIC);
736                                 if (!h5->rx_skb) {
737                                         BT_ERR("Can't allocate mem for new packet");
738                                         h5->rx_state = H5_W4_PKT_DELIMITER;
739                                         h5->rx_count = 0;
740                                         return 0;
741                                 }
742                                 h5->rx_skb->dev = (void *) hu->hdev;
743                                 break;
744                         }
745                         break;
746                 }
747         }
748         return count;
749 }
750
751         /* Arrange to retransmit all messages in the relq. */
752 static void h5_timed_event(unsigned long arg)
753 {
754         struct hci_uart *hu = (struct hci_uart *) arg;
755         struct h5_struct *h5 = hu->priv;
756         struct sk_buff *skb;
757         unsigned long flags;
758
759         BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
760
761         spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
762
763         while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
764                 h5->msgq_txseq = (h5->msgq_txseq - 1) & 0x07;
765                 skb_queue_head(&h5->rel, skb);
766         }
767
768         spin_unlock_irqrestore(&h5->unack.lock, flags);
769
770         hci_uart_tx_wakeup(hu);
771 }
772
773 static int h5_open(struct hci_uart *hu)
774 {
775         struct h5_struct *h5;
776
777         BT_DBG("hu %p", hu);
778
779         h5 = kzalloc(sizeof(*h5), GFP_ATOMIC);
780         if (!h5)
781                 return -ENOMEM;
782
783         hu->priv = h5;
784         skb_queue_head_init(&h5->unack);
785         skb_queue_head_init(&h5->rel);
786         skb_queue_head_init(&h5->unrel);
787
788         init_timer(&h5->th5);
789         h5->th5.function = h5_timed_event;
790         h5->th5.data     = (u_long) hu;
791
792         h5->rx_state = H5_W4_PKT_DELIMITER;
793
794         if (txcrc)
795                 h5->use_crc = 1;
796
797         return 0;
798 }
799
800 static int h5_close(struct hci_uart *hu)
801 {
802         struct h5_struct *h5 = hu->priv;
803         hu->priv = NULL;
804
805         BT_DBG("hu %p", hu);
806
807         skb_queue_purge(&h5->unack);
808         skb_queue_purge(&h5->rel);
809         skb_queue_purge(&h5->unrel);
810         del_timer(&h5->th5);
811
812         kfree(h5);
813         return 0;
814 }
815
816 static struct hci_uart_proto h5 = {
817         .id             = HCI_UART_3WIRE,
818         .open           = h5_open,
819         .close          = h5_close,
820         .enqueue        = h5_enqueue,
821         .dequeue        = h5_dequeue,
822         .recv           = h5_recv,
823         .flush          = h5_flush
824 };
825
826 int h5_init(void)
827 {
828         int err = hci_uart_register_proto(&h5);
829
830         if (!err)
831                 BT_INFO("HCI Realtek H5 protocol initialized");
832         else
833                 BT_ERR("HCI Realtek H5 protocol registration failed");
834
835         return err;
836 }
837
838 int h5_deinit(void)
839 {
840         return hci_uart_unregister_proto(&h5);
841 }
842
843 #if 0
844 module_param(hciextn, bool, 0644);
845 MODULE_PARM_DESC(hciextn, "Convert HCI Extensions into H5 packets-3wire test");
846 #endif