]> Pileus Git - ~andy/linux/blob - drivers/bluetooth/hci_ll.c
Bluetooth: Respect HCI_UART_DEBUG config in hci_ll.c
[~andy/linux] / drivers / bluetooth / hci_ll.c
1 /*
2  *  Texas Instruments' Bluetooth HCILL UART protocol
3  *
4  *  HCILL (HCI Low Level) is a Texas Instruments' power management
5  *  protocol extension to H4.
6  *
7  *  Copyright (C) 2007 Texas Instruments, Inc.
8  *
9  *  Written by Ohad Ben-Cohen <ohad@bencohen.org>
10  *
11  *  Acknowledgements:
12  *  This file is based on hci_h4.c, which was written
13  *  by Maxim Krasnyansky and Marcel Holtmann.
14  *
15  *  This program is free software; you can redistribute it and/or modify
16  *  it under the terms of the GNU General Public License version 2
17  *  as published by the Free Software Foundation
18  *
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software
26  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  *
28  */
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32
33 #include <linux/init.h>
34 #include <linux/sched.h>
35 #include <linux/types.h>
36 #include <linux/fcntl.h>
37 #include <linux/interrupt.h>
38 #include <linux/ptrace.h>
39 #include <linux/poll.h>
40
41 #include <linux/slab.h>
42 #include <linux/tty.h>
43 #include <linux/errno.h>
44 #include <linux/string.h>
45 #include <linux/signal.h>
46 #include <linux/ioctl.h>
47 #include <linux/skbuff.h>
48
49 #include <net/bluetooth/bluetooth.h>
50 #include <net/bluetooth/hci_core.h>
51
52 #include "hci_uart.h"
53
54 #ifndef CONFIG_BT_HCIUART_DEBUG
55 #undef  BT_DBG
56 #define BT_DBG( A... )
57 #endif
58
59 /* HCILL commands */
60 #define HCILL_GO_TO_SLEEP_IND   0x30
61 #define HCILL_GO_TO_SLEEP_ACK   0x31
62 #define HCILL_WAKE_UP_IND       0x32
63 #define HCILL_WAKE_UP_ACK       0x33
64
65 /* HCILL receiver States */
66 #define HCILL_W4_PACKET_TYPE    0
67 #define HCILL_W4_EVENT_HDR      1
68 #define HCILL_W4_ACL_HDR        2
69 #define HCILL_W4_SCO_HDR        3
70 #define HCILL_W4_DATA           4
71
72 /* HCILL states */
73 enum hcill_states_e {
74         HCILL_ASLEEP,
75         HCILL_ASLEEP_TO_AWAKE,
76         HCILL_AWAKE,
77         HCILL_AWAKE_TO_ASLEEP
78 };
79
80 struct hcill_cmd {
81         u8 cmd;
82 } __attribute__((packed));
83
84 struct ll_struct {
85         unsigned long rx_state;
86         unsigned long rx_count;
87         struct sk_buff *rx_skb;
88         struct sk_buff_head txq;
89         spinlock_t hcill_lock;          /* HCILL state lock     */
90         unsigned long hcill_state;      /* HCILL power state    */
91         struct sk_buff_head tx_wait_q;  /* HCILL wait queue     */
92 };
93
94 /*
95  * Builds and sends an HCILL command packet.
96  * These are very simple packets with only 1 cmd byte
97  */
98 static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
99 {
100         int err = 0;
101         struct sk_buff *skb = NULL;
102         struct ll_struct *ll = hu->priv;
103         struct hcill_cmd *hcill_packet;
104
105         BT_DBG("hu %p cmd 0x%x", hu, cmd);
106
107         /* allocate packet */
108         skb = bt_skb_alloc(1, GFP_ATOMIC);
109         if (!skb) {
110                 BT_ERR("cannot allocate memory for HCILL packet");
111                 err = -ENOMEM;
112                 goto out;
113         }
114
115         /* prepare packet */
116         hcill_packet = (struct hcill_cmd *) skb_put(skb, 1);
117         hcill_packet->cmd = cmd;
118         skb->dev = (void *) hu->hdev;
119
120         /* send packet */
121         skb_queue_tail(&ll->txq, skb);
122 out:
123         return err;
124 }
125
126 /* Initialize protocol */
127 static int ll_open(struct hci_uart *hu)
128 {
129         struct ll_struct *ll;
130
131         BT_DBG("hu %p", hu);
132
133         ll = kzalloc(sizeof(*ll), GFP_ATOMIC);
134         if (!ll)
135                 return -ENOMEM;
136
137         skb_queue_head_init(&ll->txq);
138         skb_queue_head_init(&ll->tx_wait_q);
139         spin_lock_init(&ll->hcill_lock);
140
141         ll->hcill_state = HCILL_AWAKE;
142
143         hu->priv = ll;
144
145         return 0;
146 }
147
148 /* Flush protocol data */
149 static int ll_flush(struct hci_uart *hu)
150 {
151         struct ll_struct *ll = hu->priv;
152
153         BT_DBG("hu %p", hu);
154
155         skb_queue_purge(&ll->tx_wait_q);
156         skb_queue_purge(&ll->txq);
157
158         return 0;
159 }
160
161 /* Close protocol */
162 static int ll_close(struct hci_uart *hu)
163 {
164         struct ll_struct *ll = hu->priv;
165
166         BT_DBG("hu %p", hu);
167
168         skb_queue_purge(&ll->tx_wait_q);
169         skb_queue_purge(&ll->txq);
170
171         if (ll->rx_skb)
172                 kfree_skb(ll->rx_skb);
173
174         hu->priv = NULL;
175
176         kfree(ll);
177
178         return 0;
179 }
180
181 /*
182  * internal function, which does common work of the device wake up process:
183  * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
184  * 2. changes internal state to HCILL_AWAKE.
185  * Note: assumes that hcill_lock spinlock is taken,
186  * shouldn't be called otherwise!
187  */
188 static void __ll_do_awake(struct ll_struct *ll)
189 {
190         struct sk_buff *skb = NULL;
191
192         while ((skb = skb_dequeue(&ll->tx_wait_q)))
193                 skb_queue_tail(&ll->txq, skb);
194
195         ll->hcill_state = HCILL_AWAKE;
196 }
197
198 /*
199  * Called upon a wake-up-indication from the device
200  */
201 static void ll_device_want_to_wakeup(struct hci_uart *hu)
202 {
203         unsigned long flags;
204         struct ll_struct *ll = hu->priv;
205
206         BT_DBG("hu %p", hu);
207
208         /* lock hcill state */
209         spin_lock_irqsave(&ll->hcill_lock, flags);
210
211         switch (ll->hcill_state) {
212         case HCILL_ASLEEP_TO_AWAKE:
213                 /*
214                  * This state means that both the host and the BRF chip
215                  * have simultaneously sent a wake-up-indication packet.
216                  * Traditionaly, in this case, receiving a wake-up-indication
217                  * was enough and an additional wake-up-ack wasn't needed.
218                  * This has changed with the BRF6350, which does require an
219                  * explicit wake-up-ack. Other BRF versions, which do not
220                  * require an explicit ack here, do accept it, thus it is
221                  * perfectly safe to always send one.
222                  */
223                 BT_DBG("dual wake-up-indication");
224                 /* deliberate fall-through - do not add break */
225         case HCILL_ASLEEP:
226                 /* acknowledge device wake up */
227                 if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
228                         BT_ERR("cannot acknowledge device wake up");
229                         goto out;
230                 }
231                 break;
232         default:
233                 /* any other state is illegal */
234                 BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
235                 break;
236         }
237
238         /* send pending packets and change state to HCILL_AWAKE */
239         __ll_do_awake(ll);
240
241 out:
242         spin_unlock_irqrestore(&ll->hcill_lock, flags);
243
244         /* actually send the packets */
245         hci_uart_tx_wakeup(hu);
246 }
247
248 /*
249  * Called upon a sleep-indication from the device
250  */
251 static void ll_device_want_to_sleep(struct hci_uart *hu)
252 {
253         unsigned long flags;
254         struct ll_struct *ll = hu->priv;
255
256         BT_DBG("hu %p", hu);
257
258         /* lock hcill state */
259         spin_lock_irqsave(&ll->hcill_lock, flags);
260
261         /* sanity check */
262         if (ll->hcill_state != HCILL_AWAKE)
263                 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
264
265         /* acknowledge device sleep */
266         if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
267                 BT_ERR("cannot acknowledge device sleep");
268                 goto out;
269         }
270
271         /* update state */
272         ll->hcill_state = HCILL_ASLEEP;
273
274 out:
275         spin_unlock_irqrestore(&ll->hcill_lock, flags);
276
277         /* actually send the sleep ack packet */
278         hci_uart_tx_wakeup(hu);
279 }
280
281 /*
282  * Called upon wake-up-acknowledgement from the device
283  */
284 static void ll_device_woke_up(struct hci_uart *hu)
285 {
286         unsigned long flags;
287         struct ll_struct *ll = hu->priv;
288
289         BT_DBG("hu %p", hu);
290
291         /* lock hcill state */
292         spin_lock_irqsave(&ll->hcill_lock, flags);
293
294         /* sanity check */
295         if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
296                 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
297
298         /* send pending packets and change state to HCILL_AWAKE */
299         __ll_do_awake(ll);
300
301         spin_unlock_irqrestore(&ll->hcill_lock, flags);
302
303         /* actually send the packets */
304         hci_uart_tx_wakeup(hu);
305 }
306
307 /* Enqueue frame for transmittion (padding, crc, etc) */
308 /* may be called from two simultaneous tasklets */
309 static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
310 {
311         unsigned long flags = 0;
312         struct ll_struct *ll = hu->priv;
313
314         BT_DBG("hu %p skb %p", hu, skb);
315
316         /* Prepend skb with frame type */
317         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
318
319         /* lock hcill state */
320         spin_lock_irqsave(&ll->hcill_lock, flags);
321
322         /* act according to current state */
323         switch (ll->hcill_state) {
324         case HCILL_AWAKE:
325                 BT_DBG("device awake, sending normally");
326                 skb_queue_tail(&ll->txq, skb);
327                 break;
328         case HCILL_ASLEEP:
329                 BT_DBG("device asleep, waking up and queueing packet");
330                 /* save packet for later */
331                 skb_queue_tail(&ll->tx_wait_q, skb);
332                 /* awake device */
333                 if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
334                         BT_ERR("cannot wake up device");
335                         break;
336                 }
337                 ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
338                 break;
339         case HCILL_ASLEEP_TO_AWAKE:
340                 BT_DBG("device waking up, queueing packet");
341                 /* transient state; just keep packet for later */
342                 skb_queue_tail(&ll->tx_wait_q, skb);
343                 break;
344         default:
345                 BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
346                 kfree_skb(skb);
347                 break;
348         }
349
350         spin_unlock_irqrestore(&ll->hcill_lock, flags);
351
352         return 0;
353 }
354
355 static inline int ll_check_data_len(struct ll_struct *ll, int len)
356 {
357         register int room = skb_tailroom(ll->rx_skb);
358
359         BT_DBG("len %d room %d", len, room);
360
361         if (!len) {
362                 hci_recv_frame(ll->rx_skb);
363         } else if (len > room) {
364                 BT_ERR("Data length is too large");
365                 kfree_skb(ll->rx_skb);
366         } else {
367                 ll->rx_state = HCILL_W4_DATA;
368                 ll->rx_count = len;
369                 return len;
370         }
371
372         ll->rx_state = HCILL_W4_PACKET_TYPE;
373         ll->rx_skb   = NULL;
374         ll->rx_count = 0;
375
376         return 0;
377 }
378
379 /* Recv data */
380 static int ll_recv(struct hci_uart *hu, void *data, int count)
381 {
382         struct ll_struct *ll = hu->priv;
383         register char *ptr;
384         struct hci_event_hdr *eh;
385         struct hci_acl_hdr   *ah;
386         struct hci_sco_hdr   *sh;
387         register int len, type, dlen;
388
389         BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
390
391         ptr = data;
392         while (count) {
393                 if (ll->rx_count) {
394                         len = min_t(unsigned int, ll->rx_count, count);
395                         memcpy(skb_put(ll->rx_skb, len), ptr, len);
396                         ll->rx_count -= len; count -= len; ptr += len;
397
398                         if (ll->rx_count)
399                                 continue;
400
401                         switch (ll->rx_state) {
402                         case HCILL_W4_DATA:
403                                 BT_DBG("Complete data");
404                                 hci_recv_frame(ll->rx_skb);
405
406                                 ll->rx_state = HCILL_W4_PACKET_TYPE;
407                                 ll->rx_skb = NULL;
408                                 continue;
409
410                         case HCILL_W4_EVENT_HDR:
411                                 eh = (struct hci_event_hdr *) ll->rx_skb->data;
412
413                                 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
414
415                                 ll_check_data_len(ll, eh->plen);
416                                 continue;
417
418                         case HCILL_W4_ACL_HDR:
419                                 ah = (struct hci_acl_hdr *) ll->rx_skb->data;
420                                 dlen = __le16_to_cpu(ah->dlen);
421
422                                 BT_DBG("ACL header: dlen %d", dlen);
423
424                                 ll_check_data_len(ll, dlen);
425                                 continue;
426
427                         case HCILL_W4_SCO_HDR:
428                                 sh = (struct hci_sco_hdr *) ll->rx_skb->data;
429
430                                 BT_DBG("SCO header: dlen %d", sh->dlen);
431
432                                 ll_check_data_len(ll, sh->dlen);
433                                 continue;
434                         }
435                 }
436
437                 /* HCILL_W4_PACKET_TYPE */
438                 switch (*ptr) {
439                 case HCI_EVENT_PKT:
440                         BT_DBG("Event packet");
441                         ll->rx_state = HCILL_W4_EVENT_HDR;
442                         ll->rx_count = HCI_EVENT_HDR_SIZE;
443                         type = HCI_EVENT_PKT;
444                         break;
445
446                 case HCI_ACLDATA_PKT:
447                         BT_DBG("ACL packet");
448                         ll->rx_state = HCILL_W4_ACL_HDR;
449                         ll->rx_count = HCI_ACL_HDR_SIZE;
450                         type = HCI_ACLDATA_PKT;
451                         break;
452
453                 case HCI_SCODATA_PKT:
454                         BT_DBG("SCO packet");
455                         ll->rx_state = HCILL_W4_SCO_HDR;
456                         ll->rx_count = HCI_SCO_HDR_SIZE;
457                         type = HCI_SCODATA_PKT;
458                         break;
459
460                 /* HCILL signals */
461                 case HCILL_GO_TO_SLEEP_IND:
462                         BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
463                         ll_device_want_to_sleep(hu);
464                         ptr++; count--;
465                         continue;
466
467                 case HCILL_GO_TO_SLEEP_ACK:
468                         /* shouldn't happen */
469                         BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
470                         ptr++; count--;
471                         continue;
472
473                 case HCILL_WAKE_UP_IND:
474                         BT_DBG("HCILL_WAKE_UP_IND packet");
475                         ll_device_want_to_wakeup(hu);
476                         ptr++; count--;
477                         continue;
478
479                 case HCILL_WAKE_UP_ACK:
480                         BT_DBG("HCILL_WAKE_UP_ACK packet");
481                         ll_device_woke_up(hu);
482                         ptr++; count--;
483                         continue;
484
485                 default:
486                         BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
487                         hu->hdev->stat.err_rx++;
488                         ptr++; count--;
489                         continue;
490                 };
491
492                 ptr++; count--;
493
494                 /* Allocate packet */
495                 ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
496                 if (!ll->rx_skb) {
497                         BT_ERR("Can't allocate mem for new packet");
498                         ll->rx_state = HCILL_W4_PACKET_TYPE;
499                         ll->rx_count = 0;
500                         return 0;
501                 }
502
503                 ll->rx_skb->dev = (void *) hu->hdev;
504                 bt_cb(ll->rx_skb)->pkt_type = type;
505         }
506
507         return count;
508 }
509
510 static struct sk_buff *ll_dequeue(struct hci_uart *hu)
511 {
512         struct ll_struct *ll = hu->priv;
513         return skb_dequeue(&ll->txq);
514 }
515
516 static struct hci_uart_proto llp = {
517         .id             = HCI_UART_LL,
518         .open           = ll_open,
519         .close          = ll_close,
520         .recv           = ll_recv,
521         .enqueue        = ll_enqueue,
522         .dequeue        = ll_dequeue,
523         .flush          = ll_flush,
524 };
525
526 int ll_init(void)
527 {
528         int err = hci_uart_register_proto(&llp);
529
530         if (!err)
531                 BT_INFO("HCILL protocol initialized");
532         else
533                 BT_ERR("HCILL protocol registration failed");
534
535         return err;
536 }
537
538 int ll_deinit(void)
539 {
540         return hci_uart_unregister_proto(&llp);
541 }