]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/bluetooth/hci_ll.c
2 * Texas Instruments' Bluetooth HCILL UART protocol
4 * HCILL (HCI Low Level) is a Texas Instruments' power management
5 * protocol extension to H4.
7 * Copyright (C) 2007 Texas Instruments, Inc.
9 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
12 * This file is based on hci_h4.c, which was written
13 * by Maxim Krasnyansky and Marcel Holtmann.
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
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.
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
30 #include <linux/module.h>
31 #include <linux/kernel.h>
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>
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>
49 #include <net/bluetooth/bluetooth.h>
50 #include <net/bluetooth/hci_core.h>
55 #define HCILL_GO_TO_SLEEP_IND 0x30
56 #define HCILL_GO_TO_SLEEP_ACK 0x31
57 #define HCILL_WAKE_UP_IND 0x32
58 #define HCILL_WAKE_UP_ACK 0x33
60 /* HCILL receiver States */
61 #define HCILL_W4_PACKET_TYPE 0
62 #define HCILL_W4_EVENT_HDR 1
63 #define HCILL_W4_ACL_HDR 2
64 #define HCILL_W4_SCO_HDR 3
65 #define HCILL_W4_DATA 4
70 HCILL_ASLEEP_TO_AWAKE
,
80 unsigned long rx_state
;
81 unsigned long rx_count
;
82 struct sk_buff
*rx_skb
;
83 struct sk_buff_head txq
;
84 spinlock_t hcill_lock
; /* HCILL state lock */
85 unsigned long hcill_state
; /* HCILL power state */
86 struct sk_buff_head tx_wait_q
; /* HCILL wait queue */
90 * Builds and sends an HCILL command packet.
91 * These are very simple packets with only 1 cmd byte
93 static int send_hcill_cmd(u8 cmd
, struct hci_uart
*hu
)
96 struct sk_buff
*skb
= NULL
;
97 struct ll_struct
*ll
= hu
->priv
;
98 struct hcill_cmd
*hcill_packet
;
100 BT_DBG("hu %p cmd 0x%x", hu
, cmd
);
102 /* allocate packet */
103 skb
= bt_skb_alloc(1, GFP_ATOMIC
);
105 BT_ERR("cannot allocate memory for HCILL packet");
111 hcill_packet
= (struct hcill_cmd
*) skb_put(skb
, 1);
112 hcill_packet
->cmd
= cmd
;
113 skb
->dev
= (void *) hu
->hdev
;
116 skb_queue_tail(&ll
->txq
, skb
);
121 /* Initialize protocol */
122 static int ll_open(struct hci_uart
*hu
)
124 struct ll_struct
*ll
;
128 ll
= kzalloc(sizeof(*ll
), GFP_KERNEL
);
132 skb_queue_head_init(&ll
->txq
);
133 skb_queue_head_init(&ll
->tx_wait_q
);
134 spin_lock_init(&ll
->hcill_lock
);
136 ll
->hcill_state
= HCILL_AWAKE
;
143 /* Flush protocol data */
144 static int ll_flush(struct hci_uart
*hu
)
146 struct ll_struct
*ll
= hu
->priv
;
150 skb_queue_purge(&ll
->tx_wait_q
);
151 skb_queue_purge(&ll
->txq
);
157 static int ll_close(struct hci_uart
*hu
)
159 struct ll_struct
*ll
= hu
->priv
;
163 skb_queue_purge(&ll
->tx_wait_q
);
164 skb_queue_purge(&ll
->txq
);
166 kfree_skb(ll
->rx_skb
);
176 * internal function, which does common work of the device wake up process:
177 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
178 * 2. changes internal state to HCILL_AWAKE.
179 * Note: assumes that hcill_lock spinlock is taken,
180 * shouldn't be called otherwise!
182 static void __ll_do_awake(struct ll_struct
*ll
)
184 struct sk_buff
*skb
= NULL
;
186 while ((skb
= skb_dequeue(&ll
->tx_wait_q
)))
187 skb_queue_tail(&ll
->txq
, skb
);
189 ll
->hcill_state
= HCILL_AWAKE
;
193 * Called upon a wake-up-indication from the device
195 static void ll_device_want_to_wakeup(struct hci_uart
*hu
)
198 struct ll_struct
*ll
= hu
->priv
;
202 /* lock hcill state */
203 spin_lock_irqsave(&ll
->hcill_lock
, flags
);
205 switch (ll
->hcill_state
) {
206 case HCILL_ASLEEP_TO_AWAKE
:
208 * This state means that both the host and the BRF chip
209 * have simultaneously sent a wake-up-indication packet.
210 * Traditionally, in this case, receiving a wake-up-indication
211 * was enough and an additional wake-up-ack wasn't needed.
212 * This has changed with the BRF6350, which does require an
213 * explicit wake-up-ack. Other BRF versions, which do not
214 * require an explicit ack here, do accept it, thus it is
215 * perfectly safe to always send one.
217 BT_DBG("dual wake-up-indication");
218 /* deliberate fall-through - do not add break */
220 /* acknowledge device wake up */
221 if (send_hcill_cmd(HCILL_WAKE_UP_ACK
, hu
) < 0) {
222 BT_ERR("cannot acknowledge device wake up");
227 /* any other state is illegal */
228 BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll
->hcill_state
);
232 /* send pending packets and change state to HCILL_AWAKE */
236 spin_unlock_irqrestore(&ll
->hcill_lock
, flags
);
238 /* actually send the packets */
239 hci_uart_tx_wakeup(hu
);
243 * Called upon a sleep-indication from the device
245 static void ll_device_want_to_sleep(struct hci_uart
*hu
)
248 struct ll_struct
*ll
= hu
->priv
;
252 /* lock hcill state */
253 spin_lock_irqsave(&ll
->hcill_lock
, flags
);
256 if (ll
->hcill_state
!= HCILL_AWAKE
)
257 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll
->hcill_state
);
259 /* acknowledge device sleep */
260 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK
, hu
) < 0) {
261 BT_ERR("cannot acknowledge device sleep");
266 ll
->hcill_state
= HCILL_ASLEEP
;
269 spin_unlock_irqrestore(&ll
->hcill_lock
, flags
);
271 /* actually send the sleep ack packet */
272 hci_uart_tx_wakeup(hu
);
276 * Called upon wake-up-acknowledgement from the device
278 static void ll_device_woke_up(struct hci_uart
*hu
)
281 struct ll_struct
*ll
= hu
->priv
;
285 /* lock hcill state */
286 spin_lock_irqsave(&ll
->hcill_lock
, flags
);
289 if (ll
->hcill_state
!= HCILL_ASLEEP_TO_AWAKE
)
290 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll
->hcill_state
);
292 /* send pending packets and change state to HCILL_AWAKE */
295 spin_unlock_irqrestore(&ll
->hcill_lock
, flags
);
297 /* actually send the packets */
298 hci_uart_tx_wakeup(hu
);
301 /* Enqueue frame for transmittion (padding, crc, etc) */
302 /* may be called from two simultaneous tasklets */
303 static int ll_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
305 unsigned long flags
= 0;
306 struct ll_struct
*ll
= hu
->priv
;
308 BT_DBG("hu %p skb %p", hu
, skb
);
310 /* Prepend skb with frame type */
311 memcpy(skb_push(skb
, 1), &bt_cb(skb
)->pkt_type
, 1);
313 /* lock hcill state */
314 spin_lock_irqsave(&ll
->hcill_lock
, flags
);
316 /* act according to current state */
317 switch (ll
->hcill_state
) {
319 BT_DBG("device awake, sending normally");
320 skb_queue_tail(&ll
->txq
, skb
);
323 BT_DBG("device asleep, waking up and queueing packet");
324 /* save packet for later */
325 skb_queue_tail(&ll
->tx_wait_q
, skb
);
327 if (send_hcill_cmd(HCILL_WAKE_UP_IND
, hu
) < 0) {
328 BT_ERR("cannot wake up device");
331 ll
->hcill_state
= HCILL_ASLEEP_TO_AWAKE
;
333 case HCILL_ASLEEP_TO_AWAKE
:
334 BT_DBG("device waking up, queueing packet");
335 /* transient state; just keep packet for later */
336 skb_queue_tail(&ll
->tx_wait_q
, skb
);
339 BT_ERR("illegal hcill state: %ld (losing packet)", ll
->hcill_state
);
344 spin_unlock_irqrestore(&ll
->hcill_lock
, flags
);
349 static inline int ll_check_data_len(struct ll_struct
*ll
, int len
)
351 int room
= skb_tailroom(ll
->rx_skb
);
353 BT_DBG("len %d room %d", len
, room
);
356 hci_recv_frame(ll
->rx_skb
);
357 } else if (len
> room
) {
358 BT_ERR("Data length is too large");
359 kfree_skb(ll
->rx_skb
);
361 ll
->rx_state
= HCILL_W4_DATA
;
366 ll
->rx_state
= HCILL_W4_PACKET_TYPE
;
374 static int ll_recv(struct hci_uart
*hu
, void *data
, int count
)
376 struct ll_struct
*ll
= hu
->priv
;
378 struct hci_event_hdr
*eh
;
379 struct hci_acl_hdr
*ah
;
380 struct hci_sco_hdr
*sh
;
383 BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu
, count
, ll
->rx_state
, ll
->rx_count
);
388 len
= min_t(unsigned int, ll
->rx_count
, count
);
389 memcpy(skb_put(ll
->rx_skb
, len
), ptr
, len
);
390 ll
->rx_count
-= len
; count
-= len
; ptr
+= len
;
395 switch (ll
->rx_state
) {
397 BT_DBG("Complete data");
398 hci_recv_frame(ll
->rx_skb
);
400 ll
->rx_state
= HCILL_W4_PACKET_TYPE
;
404 case HCILL_W4_EVENT_HDR
:
405 eh
= hci_event_hdr(ll
->rx_skb
);
407 BT_DBG("Event header: evt 0x%2.2x plen %d", eh
->evt
, eh
->plen
);
409 ll_check_data_len(ll
, eh
->plen
);
412 case HCILL_W4_ACL_HDR
:
413 ah
= hci_acl_hdr(ll
->rx_skb
);
414 dlen
= __le16_to_cpu(ah
->dlen
);
416 BT_DBG("ACL header: dlen %d", dlen
);
418 ll_check_data_len(ll
, dlen
);
421 case HCILL_W4_SCO_HDR
:
422 sh
= hci_sco_hdr(ll
->rx_skb
);
424 BT_DBG("SCO header: dlen %d", sh
->dlen
);
426 ll_check_data_len(ll
, sh
->dlen
);
431 /* HCILL_W4_PACKET_TYPE */
434 BT_DBG("Event packet");
435 ll
->rx_state
= HCILL_W4_EVENT_HDR
;
436 ll
->rx_count
= HCI_EVENT_HDR_SIZE
;
437 type
= HCI_EVENT_PKT
;
440 case HCI_ACLDATA_PKT
:
441 BT_DBG("ACL packet");
442 ll
->rx_state
= HCILL_W4_ACL_HDR
;
443 ll
->rx_count
= HCI_ACL_HDR_SIZE
;
444 type
= HCI_ACLDATA_PKT
;
447 case HCI_SCODATA_PKT
:
448 BT_DBG("SCO packet");
449 ll
->rx_state
= HCILL_W4_SCO_HDR
;
450 ll
->rx_count
= HCI_SCO_HDR_SIZE
;
451 type
= HCI_SCODATA_PKT
;
455 case HCILL_GO_TO_SLEEP_IND
:
456 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
457 ll_device_want_to_sleep(hu
);
461 case HCILL_GO_TO_SLEEP_ACK
:
462 /* shouldn't happen */
463 BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll
->hcill_state
);
467 case HCILL_WAKE_UP_IND
:
468 BT_DBG("HCILL_WAKE_UP_IND packet");
469 ll_device_want_to_wakeup(hu
);
473 case HCILL_WAKE_UP_ACK
:
474 BT_DBG("HCILL_WAKE_UP_ACK packet");
475 ll_device_woke_up(hu
);
480 BT_ERR("Unknown HCI packet type %2.2x", (__u8
)*ptr
);
481 hu
->hdev
->stat
.err_rx
++;
488 /* Allocate packet */
489 ll
->rx_skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
491 BT_ERR("Can't allocate mem for new packet");
492 ll
->rx_state
= HCILL_W4_PACKET_TYPE
;
497 ll
->rx_skb
->dev
= (void *) hu
->hdev
;
498 bt_cb(ll
->rx_skb
)->pkt_type
= type
;
504 static struct sk_buff
*ll_dequeue(struct hci_uart
*hu
)
506 struct ll_struct
*ll
= hu
->priv
;
507 return skb_dequeue(&ll
->txq
);
510 static struct hci_uart_proto llp
= {
515 .enqueue
= ll_enqueue
,
516 .dequeue
= ll_dequeue
,
520 int __init
ll_init(void)
522 int err
= hci_uart_register_proto(&llp
);
525 BT_INFO("HCILL protocol initialized");
527 BT_ERR("HCILL protocol registration failed");
532 int __exit
ll_deinit(void)
534 return hci_uart_unregister_proto(&llp
);