Blame view

drivers/bluetooth/hci_bcsp.c 18.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   *
   *  Bluetooth HCI UART driver
   *
   *  Copyright (C) 2002-2003  Fabrizio Gennari <fabrizio.gennari@philips.com>
   *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
   *
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
27
  #include <linux/module.h>
  
  #include <linux/kernel.h>
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <linux/types.h>
  #include <linux/fcntl.h>
  #include <linux/interrupt.h>
  #include <linux/ptrace.h>
  #include <linux/poll.h>
  
  #include <linux/slab.h>
  #include <linux/tty.h>
  #include <linux/errno.h>
  #include <linux/string.h>
  #include <linux/signal.h>
  #include <linux/ioctl.h>
  #include <linux/skbuff.h>
c5ec51401   Harvey Harrison   bluetooth: hci_bc...
41
42
  #include <linux/bitrev.h>
  #include <asm/unaligned.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
  
  #include <net/bluetooth/bluetooth.h>
  #include <net/bluetooth/hci_core.h>
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
46

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  #include "hci_uart.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48

0372a6627   Marcel Holtmann   [Bluetooth] Clean...
49
  #define VERSION "0.3"
90ab5ee94   Rusty Russell   module_param: mak...
50
51
  static bool txcrc = 1;
  static bool hciextn = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52

0372a6627   Marcel Holtmann   [Bluetooth] Clean...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  #define BCSP_TXWINSIZE	4
  
  #define BCSP_ACK_PKT	0x05
  #define BCSP_LE_PKT	0x06
  
  struct bcsp_struct {
  	struct sk_buff_head unack;	/* Unack'ed packets queue */
  	struct sk_buff_head rel;	/* Reliable packets queue */
  	struct sk_buff_head unrel;	/* Unreliable packets queue */
  
  	unsigned long rx_count;
  	struct	sk_buff *rx_skb;
  	u8	rxseq_txack;		/* rxseq == txack. */
  	u8	rxack;			/* Last packet sent by us that the peer ack'ed */
  	struct	timer_list tbcsp;
  
  	enum {
  		BCSP_W4_PKT_DELIMITER,
  		BCSP_W4_PKT_START,
  		BCSP_W4_BCSP_HDR,
  		BCSP_W4_DATA,
  		BCSP_W4_CRC
  	} rx_state;
  
  	enum {
  		BCSP_ESCSTATE_NOESC,
  		BCSP_ESCSTATE_ESC
  	} rx_esc_state;
  
  	u8	use_crc;
  	u16	message_crc;
  	u8	txack_req;		/* Do we need to send ack's to the peer? */
  
  	/* Reliable packet sequence number - used to assign seq to each rel pkt. */
  	u8	msgq_txseq;
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  /* ---- BCSP CRC calculation ---- */
  
  /* Table for calculating CRC for polynomial 0x1021, LSB processed first,
  initial value 0xffff, bits shifted in reverse order. */
  
  static const u16 crc_table[] = {
  	0x0000, 0x1081, 0x2102, 0x3183,
  	0x4204, 0x5285, 0x6306, 0x7387,
  	0x8408, 0x9489, 0xa50a, 0xb58b,
  	0xc60c, 0xd68d, 0xe70e, 0xf78f
  };
  
  /* Initialise the crc calculator */
  #define BCSP_CRC_INIT(x) x = 0xffff
  
  /*
     Update crc with next data byte
  
     Implementation note
          The data byte is treated as two nibbles.  The crc is generated
          in reverse, i.e., bits are fed into the register from the top.
  */
  static void bcsp_crc_update(u16 *crc, u8 d)
  {
  	u16 reg = *crc;
  
  	reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
  	reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
  
  	*crc = reg;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
123
124
  /* ---- BCSP core ---- */
  
  static void bcsp_slip_msgdelim(struct sk_buff *skb)
  {
  	const char pkt_delim = 0xc0;
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
125

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  	memcpy(skb_put(skb, 1), &pkt_delim, 1);
  }
  
  static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c)
  {
  	const char esc_c0[2] = { 0xdb, 0xdc };
  	const char esc_db[2] = { 0xdb, 0xdd };
  
  	switch (c) {
  	case 0xc0:
  		memcpy(skb_put(skb, 2), &esc_c0, 2);
  		break;
  	case 0xdb:
  		memcpy(skb_put(skb, 2), &esc_db, 2);
  		break;
  	default:
  		memcpy(skb_put(skb, 1), &c, 1);
  	}
  }
  
  static int bcsp_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  {
  	struct bcsp_struct *bcsp = hu->priv;
  
  	if (skb->len > 0xFFF) {
  		BT_ERR("Packet too long");
  		kfree_skb(skb);
  		return 0;
  	}
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
155
  	switch (bt_cb(skb)->pkt_type) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  	case HCI_ACLDATA_PKT:
  	case HCI_COMMAND_PKT:
  		skb_queue_tail(&bcsp->rel, skb);
  		break;
  
  	case HCI_SCODATA_PKT:
  		skb_queue_tail(&bcsp->unrel, skb);
  		break;
  
  	default:
  		BT_ERR("Unknown packet type");
  		kfree_skb(skb);
  		break;
  	}
  
  	return 0;
  }
  
  static struct sk_buff *bcsp_prepare_pkt(struct bcsp_struct *bcsp, u8 *data,
  		int len, int pkt_type)
  {
  	struct sk_buff *nskb;
  	u8 hdr[4], chan;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  	u16 BCSP_CRC_INIT(bcsp_txmsg_crc);
20dd6f59d   Marcel Holtmann   [Bluetooth] Remov...
180
  	int rel, i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  
  	switch (pkt_type) {
  	case HCI_ACLDATA_PKT:
  		chan = 6;	/* BCSP ACL channel */
  		rel = 1;	/* reliable channel */
  		break;
  	case HCI_COMMAND_PKT:
  		chan = 5;	/* BCSP cmd/evt channel */
  		rel = 1;	/* reliable channel */
  		break;
  	case HCI_SCODATA_PKT:
  		chan = 7;	/* BCSP SCO channel */
  		rel = 0;	/* unreliable channel */
  		break;
  	case BCSP_LE_PKT:
  		chan = 1;	/* BCSP LE channel */
  		rel = 0;	/* unreliable channel */
  		break;
  	case BCSP_ACK_PKT:
  		chan = 0;	/* BCSP internal channel */
  		rel = 0;	/* unreliable channel */
  		break;
  	default:
  		BT_ERR("Unknown packet type");
  		return NULL;
  	}
  
  	if (hciextn && chan == 5) {
c5ec51401   Harvey Harrison   bluetooth: hci_bc...
209
  		__le16 opcode = ((struct hci_command_hdr *)data)->opcode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210

a9de92480   Marcel Holtmann   [Bluetooth] Switc...
211
  		/* Vendor specific commands */
c5ec51401   Harvey Harrison   bluetooth: hci_bc...
212
  		if (hci_opcode_ogf(__le16_to_cpu(opcode)) == 0x3f) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  			u8 desc = *(data + HCI_COMMAND_HDR_SIZE);
  			if ((desc & 0xf0) == 0xc0) {
  				data += HCI_COMMAND_HDR_SIZE + 1;
  				len  -= HCI_COMMAND_HDR_SIZE + 1;
  				chan = desc & 0x0f;
  			}
  		}
  	}
  
  	/* Max len of packet: (original len +4(bcsp hdr) +2(crc))*2
  	   (because bytes 0xc0 and 0xdb are escaped, worst case is
  	   when the packet is all made of 0xc0 and 0xdb :) )
  	   + 2 (0xc0 delimiters at start and end). */
  
  	nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
  	if (!nskb)
  		return NULL;
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
230
  	bt_cb(nskb)->pkt_type = pkt_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
232
233
234
235
236
237
238
239
240
  
  	bcsp_slip_msgdelim(nskb);
  
  	hdr[0] = bcsp->rxseq_txack << 3;
  	bcsp->txack_req = 0;
  	BT_DBG("We request packet no %u to card", bcsp->rxseq_txack);
  
  	if (rel) {
  		hdr[0] |= 0x80 + bcsp->msgq_txseq;
  		BT_DBG("Sending packet with seqno %u", bcsp->msgq_txseq);
dd1589a43   David Howells   Bluetooth: Fix ab...
241
  		bcsp->msgq_txseq = (bcsp->msgq_txseq + 1) & 0x07;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
  	}
20dd6f59d   Marcel Holtmann   [Bluetooth] Remov...
243
244
245
  
  	if (bcsp->use_crc)
  		hdr[0] |= 0x40;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
247
248
249
250
251
252
253
  
  	hdr[1] = ((len << 4) & 0xff) | chan;
  	hdr[2] = len >> 4;
  	hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);
  
  	/* Put BCSP header */
  	for (i = 0; i < 4; i++) {
  		bcsp_slip_one_byte(nskb, hdr[i]);
20dd6f59d   Marcel Holtmann   [Bluetooth] Remov...
254
255
256
  
  		if (bcsp->use_crc)
  			bcsp_crc_update(&bcsp_txmsg_crc, hdr[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
258
259
260
261
  	}
  
  	/* Put payload */
  	for (i = 0; i < len; i++) {
  		bcsp_slip_one_byte(nskb, data[i]);
20dd6f59d   Marcel Holtmann   [Bluetooth] Remov...
262
263
264
  
  		if (bcsp->use_crc)
  			bcsp_crc_update(&bcsp_txmsg_crc, data[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
  	/* Put CRC */
20dd6f59d   Marcel Holtmann   [Bluetooth] Remov...
267
  	if (bcsp->use_crc) {
c5ec51401   Harvey Harrison   bluetooth: hci_bc...
268
  		bcsp_txmsg_crc = bitrev16(bcsp_txmsg_crc);
20dd6f59d   Marcel Holtmann   [Bluetooth] Remov...
269
270
271
  		bcsp_slip_one_byte(nskb, (u8) ((bcsp_txmsg_crc >> 8) & 0x00ff));
  		bcsp_slip_one_byte(nskb, (u8) (bcsp_txmsg_crc & 0x00ff));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  
  	bcsp_slip_msgdelim(nskb);
  	return nskb;
  }
  
  /* This is a rewrite of pkt_avail in ABCSP */
  static struct sk_buff *bcsp_dequeue(struct hci_uart *hu)
  {
  	struct bcsp_struct *bcsp = hu->priv;
  	unsigned long flags;
  	struct sk_buff *skb;
  	
  	/* First of all, check for unreliable messages in the queue,
  	   since they have priority */
  
  	if ((skb = skb_dequeue(&bcsp->unrel)) != NULL) {
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
288
  		struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len, bt_cb(skb)->pkt_type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
291
292
293
294
295
296
297
298
299
300
  		if (nskb) {
  			kfree_skb(skb);
  			return nskb;
  		} else {
  			skb_queue_head(&bcsp->unrel, skb);
  			BT_ERR("Could not dequeue pkt because alloc_skb failed");
  		}
  	}
  
  	/* Now, try to send a reliable pkt. We can only send a
  	   reliable packet if the number of packets sent but not yet ack'ed
  	   is < than the winsize */
f89d75f22   Peter Zijlstra   [PATCH] lockdep: ...
301
  	spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
303
  
  	if (bcsp->unack.qlen < BCSP_TXWINSIZE && (skb = skb_dequeue(&bcsp->rel)) != NULL) {
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
304
  		struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len, bt_cb(skb)->pkt_type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
306
307
308
309
310
311
312
313
314
315
316
  		if (nskb) {
  			__skb_queue_tail(&bcsp->unack, skb);
  			mod_timer(&bcsp->tbcsp, jiffies + HZ / 4);
  			spin_unlock_irqrestore(&bcsp->unack.lock, flags);
  			return nskb;
  		} else {
  			skb_queue_head(&bcsp->rel, skb);
  			BT_ERR("Could not dequeue pkt because alloc_skb failed");
  		}
  	}
  
  	spin_unlock_irqrestore(&bcsp->unack.lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  	/* We could not send a reliable packet, either because there are
  	   none or because there are too many unack'ed pkts. Did we receive
  	   any packets we have not acknowledged yet ? */
  
  	if (bcsp->txack_req) {
  		/* if so, craft an empty ACK pkt and send it on BCSP unreliable
  		   channel 0 */
  		struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, NULL, 0, BCSP_ACK_PKT);
  		return nskb;
  	}
  
  	/* We have nothing to send */
  	return NULL;
  }
  
  static int bcsp_flush(struct hci_uart *hu)
  {
  	BT_DBG("hu %p", hu);
  	return 0;
  }
  
  /* Remove ack'ed packets */
  static void bcsp_pkt_cull(struct bcsp_struct *bcsp)
  {
8fc5387cb   David S. Miller   bluetooth: hci_bc...
341
  	struct sk_buff *skb, *tmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
  	int i, pkts_to_be_removed;
  	u8 seqno;
  
  	spin_lock_irqsave(&bcsp->unack.lock, flags);
8fc5387cb   David S. Miller   bluetooth: hci_bc...
347
  	pkts_to_be_removed = skb_queue_len(&bcsp->unack);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
350
351
352
353
354
355
356
357
358
359
360
  	seqno = bcsp->msgq_txseq;
  
  	while (pkts_to_be_removed) {
  		if (bcsp->rxack == seqno)
  			break;
  		pkts_to_be_removed--;
  		seqno = (seqno - 1) & 0x07;
  	}
  
  	if (bcsp->rxack != seqno)
  		BT_ERR("Peer acked invalid packet");
  
  	BT_DBG("Removing %u pkts out of %u, up to seqno %u",
8fc5387cb   David S. Miller   bluetooth: hci_bc...
361
362
  	       pkts_to_be_removed, skb_queue_len(&bcsp->unack),
  	       (seqno - 1) & 0x07);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363

8fc5387cb   David S. Miller   bluetooth: hci_bc...
364
365
  	i = 0;
  	skb_queue_walk_safe(&bcsp->unack, skb, tmp) {
d2e353f7c   Wending Weng   Bluetooth: Fix fa...
366
  		if (i >= pkts_to_be_removed)
8fc5387cb   David S. Miller   bluetooth: hci_bc...
367
  			break;
d2e353f7c   Wending Weng   Bluetooth: Fix fa...
368
  		i++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
371
  		__skb_unlink(skb, &bcsp->unack);
  		kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
  	}
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
373

8fc5387cb   David S. Miller   bluetooth: hci_bc...
374
  	if (skb_queue_empty(&bcsp->unack))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
  		del_timer(&bcsp->tbcsp);
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
376

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  	spin_unlock_irqrestore(&bcsp->unack.lock, flags);
  
  	if (i != pkts_to_be_removed)
  		BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed);
  }
  
  /* Handle BCSP link-establishment packets. When we
     detect a "sync" packet, symptom that the BT module has reset,
     we do nothing :) (yet) */
  static void bcsp_handle_le_pkt(struct hci_uart *hu)
  {
  	struct bcsp_struct *bcsp = hu->priv;
  	u8 conf_pkt[4]     = { 0xad, 0xef, 0xac, 0xed };
  	u8 conf_rsp_pkt[4] = { 0xde, 0xad, 0xd0, 0xd0 };
  	u8 sync_pkt[4]     = { 0xda, 0xdc, 0xed, 0xed };
  
  	/* spot "conf" pkts and reply with a "conf rsp" pkt */
  	if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
  			!memcmp(&bcsp->rx_skb->data[4], conf_pkt, 4)) {
  		struct sk_buff *nskb = alloc_skb(4, GFP_ATOMIC);
  
  		BT_DBG("Found a LE conf pkt");
  		if (!nskb)
  			return;
  		memcpy(skb_put(nskb, 4), conf_rsp_pkt, 4);
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
402
  		bt_cb(nskb)->pkt_type = BCSP_LE_PKT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
  
  		skb_queue_head(&bcsp->unrel, nskb);
  		hci_uart_tx_wakeup(hu);
  	}
  	/* Spot "sync" pkts. If we find one...disaster! */
  	else if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
  			!memcmp(&bcsp->rx_skb->data[4], sync_pkt, 4)) {
  		BT_ERR("Found a LE sync pkt, card has reset");
  	}
  }
  
  static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char byte)
  {
  	const u8 c0 = 0xc0, db = 0xdb;
  
  	switch (bcsp->rx_esc_state) {
  	case BCSP_ESCSTATE_NOESC:
  		switch (byte) {
  		case 0xdb:
  			bcsp->rx_esc_state = BCSP_ESCSTATE_ESC;
  			break;
  		default:
  			memcpy(skb_put(bcsp->rx_skb, 1), &byte, 1);
  			if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
  					bcsp->rx_state != BCSP_W4_CRC)
  				bcsp_crc_update(&bcsp->message_crc, byte);
  			bcsp->rx_count--;
  		}
  		break;
  
  	case BCSP_ESCSTATE_ESC:
  		switch (byte) {
  		case 0xdc:
  			memcpy(skb_put(bcsp->rx_skb, 1), &c0, 1);
  			if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
  					bcsp->rx_state != BCSP_W4_CRC)
  				bcsp_crc_update(&bcsp-> message_crc, 0xc0);
  			bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
  			bcsp->rx_count--;
  			break;
  
  		case 0xdd:
  			memcpy(skb_put(bcsp->rx_skb, 1), &db, 1);
  			if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
  					bcsp->rx_state != BCSP_W4_CRC) 
  				bcsp_crc_update(&bcsp-> message_crc, 0xdb);
  			bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
  			bcsp->rx_count--;
  			break;
  
  		default:
  			BT_ERR ("Invalid byte %02x after esc byte", byte);
  			kfree_skb(bcsp->rx_skb);
  			bcsp->rx_skb = NULL;
  			bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  			bcsp->rx_count = 0;
  		}
  	}
  }
858119e15   Arjan van de Ven   [PATCH] Unlinline...
462
  static void bcsp_complete_rx_pkt(struct hci_uart *hu)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  {
  	struct bcsp_struct *bcsp = hu->priv;
  	int pass_up;
  
  	if (bcsp->rx_skb->data[0] & 0x80) {	/* reliable pkt */
  		BT_DBG("Received seqno %u from card", bcsp->rxseq_txack);
  		bcsp->rxseq_txack++;
  		bcsp->rxseq_txack %= 0x8;
  		bcsp->txack_req    = 1;
  
  		/* If needed, transmit an ack pkt */
  		hci_uart_tx_wakeup(hu);
  	}
  
  	bcsp->rxack = (bcsp->rx_skb->data[0] >> 3) & 0x07;
  	BT_DBG("Request for pkt %u from card", bcsp->rxack);
  
  	bcsp_pkt_cull(bcsp);
  	if ((bcsp->rx_skb->data[1] & 0x0f) == 6 &&
  			bcsp->rx_skb->data[0] & 0x80) {
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
483
  		bt_cb(bcsp->rx_skb)->pkt_type = HCI_ACLDATA_PKT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
485
486
  		pass_up = 1;
  	} else if ((bcsp->rx_skb->data[1] & 0x0f) == 5 &&
  			bcsp->rx_skb->data[0] & 0x80) {
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
487
  		bt_cb(bcsp->rx_skb)->pkt_type = HCI_EVENT_PKT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
488
489
  		pass_up = 1;
  	} else if ((bcsp->rx_skb->data[1] & 0x0f) == 7) {
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
490
  		bt_cb(bcsp->rx_skb)->pkt_type = HCI_SCODATA_PKT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  		pass_up = 1;
  	} else if ((bcsp->rx_skb->data[1] & 0x0f) == 1 &&
  			!(bcsp->rx_skb->data[0] & 0x80)) {
  		bcsp_handle_le_pkt(hu);
  		pass_up = 0;
  	} else
  		pass_up = 0;
  
  	if (!pass_up) {
  		struct hci_event_hdr hdr;
  		u8 desc = (bcsp->rx_skb->data[1] & 0x0f);
  
  		if (desc != 0 && desc != 1) {
  			if (hciextn) {
  				desc |= 0xc0;
  				skb_pull(bcsp->rx_skb, 4);
  				memcpy(skb_push(bcsp->rx_skb, 1), &desc, 1);
  
  				hdr.evt = 0xff;
  				hdr.plen = bcsp->rx_skb->len;
  				memcpy(skb_push(bcsp->rx_skb, HCI_EVENT_HDR_SIZE), &hdr, HCI_EVENT_HDR_SIZE);
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
512
  				bt_cb(bcsp->rx_skb)->pkt_type = HCI_EVENT_PKT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  
  				hci_recv_frame(bcsp->rx_skb);
  			} else {
  				BT_ERR ("Packet for unknown channel (%u %s)",
  					bcsp->rx_skb->data[1] & 0x0f,
  					bcsp->rx_skb->data[0] & 0x80 ? 
  					"reliable" : "unreliable");
  				kfree_skb(bcsp->rx_skb);
  			}
  		} else
  			kfree_skb(bcsp->rx_skb);
  	} else {
  		/* Pull out BCSP hdr */
  		skb_pull(bcsp->rx_skb, 4);
  
  		hci_recv_frame(bcsp->rx_skb);
  	}
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
530

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531
532
533
  	bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  	bcsp->rx_skb = NULL;
  }
c5ec51401   Harvey Harrison   bluetooth: hci_bc...
534
535
536
537
  static u16 bscp_get_crc(struct bcsp_struct *bcsp)
  {
  	return get_unaligned_be16(&bcsp->rx_skb->data[bcsp->rx_skb->len - 2]);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  /* Recv data */
  static int bcsp_recv(struct hci_uart *hu, void *data, int count)
  {
  	struct bcsp_struct *bcsp = hu->priv;
  	register unsigned char *ptr;
  
  	BT_DBG("hu %p count %d rx_state %d rx_count %ld", 
  		hu, count, bcsp->rx_state, bcsp->rx_count);
  
  	ptr = data;
  	while (count) {
  		if (bcsp->rx_count) {
  			if (*ptr == 0xc0) {
  				BT_ERR("Short BCSP packet");
  				kfree_skb(bcsp->rx_skb);
  				bcsp->rx_state = BCSP_W4_PKT_START;
  				bcsp->rx_count = 0;
  			} else
  				bcsp_unslip_one_byte(bcsp, *ptr);
  
  			ptr++; count--;
  			continue;
  		}
  
  		switch (bcsp->rx_state) {
  		case BCSP_W4_BCSP_HDR:
  			if ((0xff & (u8) ~ (bcsp->rx_skb->data[0] + bcsp->rx_skb->data[1] +
  					bcsp->rx_skb->data[2])) != bcsp->rx_skb->data[3]) {
  				BT_ERR("Error in BCSP hdr checksum");
  				kfree_skb(bcsp->rx_skb);
  				bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  				bcsp->rx_count = 0;
  				continue;
  			}
  			if (bcsp->rx_skb->data[0] & 0x80	/* reliable pkt */
  			    		&& (bcsp->rx_skb->data[0] & 0x07) != bcsp->rxseq_txack) {
  				BT_ERR ("Out-of-order packet arrived, got %u expected %u",
  					bcsp->rx_skb->data[0] & 0x07, bcsp->rxseq_txack);
  
  				kfree_skb(bcsp->rx_skb);
  				bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  				bcsp->rx_count = 0;
  				continue;
  			}
  			bcsp->rx_state = BCSP_W4_DATA;
  			bcsp->rx_count = (bcsp->rx_skb->data[1] >> 4) + 
  					(bcsp->rx_skb->data[2] << 4);	/* May be 0 */
  			continue;
  
  		case BCSP_W4_DATA:
  			if (bcsp->rx_skb->data[0] & 0x40) {	/* pkt with crc */
  				bcsp->rx_state = BCSP_W4_CRC;
  				bcsp->rx_count = 2;
  			} else
  				bcsp_complete_rx_pkt(hu);
  			continue;
  
  		case BCSP_W4_CRC:
c5ec51401   Harvey Harrison   bluetooth: hci_bc...
596
  			if (bitrev16(bcsp->message_crc) != bscp_get_crc(bcsp)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
  				BT_ERR ("Checksum failed: computed %04x received %04x",
c5ec51401   Harvey Harrison   bluetooth: hci_bc...
598
599
  					bitrev16(bcsp->message_crc),
  					bscp_get_crc(bcsp));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  
  				kfree_skb(bcsp->rx_skb);
  				bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  				bcsp->rx_count = 0;
  				continue;
  			}
  			skb_trim(bcsp->rx_skb, bcsp->rx_skb->len - 2);
  			bcsp_complete_rx_pkt(hu);
  			continue;
  
  		case BCSP_W4_PKT_DELIMITER:
  			switch (*ptr) {
  			case 0xc0:
  				bcsp->rx_state = BCSP_W4_PKT_START;
  				break;
  			default:
  				/*BT_ERR("Ignoring byte %02x", *ptr);*/
  				break;
  			}
  			ptr++; count--;
  			break;
  
  		case BCSP_W4_PKT_START:
  			switch (*ptr) {
  			case 0xc0:
  				ptr++; count--;
  				break;
  
  			default:
  				bcsp->rx_state = BCSP_W4_BCSP_HDR;
  				bcsp->rx_count = 4;
  				bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
  				BCSP_CRC_INIT(bcsp->message_crc);
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
633

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
  				/* Do not increment ptr or decrement count
  				 * Allocate packet. Max len of a BCSP pkt= 
  				 * 0xFFF (payload) +4 (header) +2 (crc) */
  
  				bcsp->rx_skb = bt_skb_alloc(0x1005, GFP_ATOMIC);
  				if (!bcsp->rx_skb) {
  					BT_ERR("Can't allocate mem for new packet");
  					bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  					bcsp->rx_count = 0;
  					return 0;
  				}
  				bcsp->rx_skb->dev = (void *) hu->hdev;
  				break;
  			}
  			break;
  		}
  	}
  	return count;
  }
  
  	/* Arrange to retransmit all messages in the relq. */
  static void bcsp_timed_event(unsigned long arg)
  {
  	struct hci_uart *hu = (struct hci_uart *) arg;
  	struct bcsp_struct *bcsp = hu->priv;
  	struct sk_buff *skb;
  	unsigned long flags;
  
  	BT_DBG("hu %p retransmitting %u pkts", hu, bcsp->unack.qlen);
f89d75f22   Peter Zijlstra   [PATCH] lockdep: ...
663
  	spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
  
  	while ((skb = __skb_dequeue_tail(&bcsp->unack)) != NULL) {
  		bcsp->msgq_txseq = (bcsp->msgq_txseq - 1) & 0x07;
  		skb_queue_head(&bcsp->rel, skb);
  	}
  
  	spin_unlock_irqrestore(&bcsp->unack.lock, flags);
  
  	hci_uart_tx_wakeup(hu);
  }
  
  static int bcsp_open(struct hci_uart *hu)
  {
  	struct bcsp_struct *bcsp;
  
  	BT_DBG("hu %p", hu);
089b1dbbd   Deepak Saxena   [PATCH] bluetooth...
680
  	bcsp = kzalloc(sizeof(*bcsp), GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
681
682
  	if (!bcsp)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683
684
685
686
687
688
689
690
691
692
693
  
  	hu->priv = bcsp;
  	skb_queue_head_init(&bcsp->unack);
  	skb_queue_head_init(&bcsp->rel);
  	skb_queue_head_init(&bcsp->unrel);
  
  	init_timer(&bcsp->tbcsp);
  	bcsp->tbcsp.function = bcsp_timed_event;
  	bcsp->tbcsp.data     = (u_long) hu;
  
  	bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
20dd6f59d   Marcel Holtmann   [Bluetooth] Remov...
694
695
  	if (txcrc)
  		bcsp->use_crc = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
  	return 0;
  }
  
  static int bcsp_close(struct hci_uart *hu)
  {
  	struct bcsp_struct *bcsp = hu->priv;
  	hu->priv = NULL;
  
  	BT_DBG("hu %p", hu);
  
  	skb_queue_purge(&bcsp->unack);
  	skb_queue_purge(&bcsp->rel);
  	skb_queue_purge(&bcsp->unrel);
  	del_timer(&bcsp->tbcsp);
  
  	kfree(bcsp);
  	return 0;
  }
  
  static struct hci_uart_proto bcsp = {
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
716
717
718
719
720
721
722
  	.id		= HCI_UART_BCSP,
  	.open		= bcsp_open,
  	.close		= bcsp_close,
  	.enqueue	= bcsp_enqueue,
  	.dequeue	= bcsp_dequeue,
  	.recv		= bcsp_recv,
  	.flush		= bcsp_flush
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
723
  };
f2b94bb9e   Gustavo F. Padovan   Bluetooth: Add __...
724
  int __init bcsp_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
726
  {
  	int err = hci_uart_register_proto(&bcsp);
0372a6627   Marcel Holtmann   [Bluetooth] Clean...
727

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
728
729
730
731
732
733
734
  	if (!err)
  		BT_INFO("HCI BCSP protocol initialized");
  	else
  		BT_ERR("HCI BCSP protocol registration failed");
  
  	return err;
  }
f2b94bb9e   Gustavo F. Padovan   Bluetooth: Add __...
735
  int __exit bcsp_deinit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
  {
  	return hci_uart_unregister_proto(&bcsp);
  }
20dd6f59d   Marcel Holtmann   [Bluetooth] Remov...
739
740
  module_param(txcrc, bool, 0644);
  MODULE_PARM_DESC(txcrc, "Transmit CRC with every BCSP packet");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
742
  module_param(hciextn, bool, 0644);
  MODULE_PARM_DESC(hciextn, "Convert HCI Extensions into BCSP packets");