YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 3 | Copyright (c) 2000-2001, 2010-2012 The Linux Foundation. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI core. */ |
| 26 | |
S.Çağlar Onur | 8245302 | 2008-02-17 23:25:57 -0800 | [diff] [blame] | 27 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/kmod.h> |
| 30 | |
| 31 | #include <linux/types.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/sched.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/poll.h> |
| 37 | #include <linux/fcntl.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/skbuff.h> |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 40 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/notifier.h> |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 43 | #include <linux/rfkill.h> |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 44 | #include <linux/timer.h> |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 45 | #include <linux/crypto.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <net/sock.h> |
| 47 | |
| 48 | #include <asm/system.h> |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 49 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <asm/unaligned.h> |
| 51 | |
| 52 | #include <net/bluetooth/bluetooth.h> |
| 53 | #include <net/bluetooth/hci_core.h> |
| 54 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 55 | #define AUTO_OFF_TIMEOUT 2000 |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static void hci_cmd_task(unsigned long arg); |
| 58 | static void hci_rx_task(unsigned long arg); |
| 59 | static void hci_tx_task(unsigned long arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | static DEFINE_RWLOCK(hci_task_lock); |
| 62 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 63 | static bool enable_smp = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* HCI device list */ |
| 66 | LIST_HEAD(hci_dev_list); |
| 67 | DEFINE_RWLOCK(hci_dev_list_lock); |
| 68 | |
| 69 | /* HCI callback list */ |
| 70 | LIST_HEAD(hci_cb_list); |
| 71 | DEFINE_RWLOCK(hci_cb_list_lock); |
| 72 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 73 | /* AMP Manager event callbacks */ |
| 74 | LIST_HEAD(amp_mgr_cb_list); |
| 75 | DEFINE_RWLOCK(amp_mgr_cb_list_lock); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* HCI protocols */ |
| 78 | #define HCI_MAX_PROTO 2 |
| 79 | struct hci_proto *hci_proto[HCI_MAX_PROTO]; |
| 80 | |
| 81 | /* HCI notifiers list */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 82 | static ATOMIC_NOTIFIER_HEAD(hci_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* ---- HCI notifications ---- */ |
| 85 | |
| 86 | int hci_register_notifier(struct notifier_block *nb) |
| 87 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 88 | return atomic_notifier_chain_register(&hci_notifier, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int hci_unregister_notifier(struct notifier_block *nb) |
| 92 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 93 | return atomic_notifier_chain_unregister(&hci_notifier, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Marcel Holtmann | 6516455 | 2005-10-28 19:20:48 +0200 | [diff] [blame] | 96 | static void hci_notify(struct hci_dev *hdev, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 98 | atomic_notifier_call_chain(&hci_notifier, event, hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* ---- HCI requests ---- */ |
| 102 | |
Johan Hedberg | 23bb576 | 2010-12-21 23:01:27 +0200 | [diff] [blame] | 103 | void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
Johan Hedberg | 23bb576 | 2010-12-21 23:01:27 +0200 | [diff] [blame] | 105 | BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result); |
| 106 | |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 107 | /* If this is the init phase check if the completed command matches |
| 108 | * the last init command, and if not just return. |
| 109 | */ |
| 110 | if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd) |
Johan Hedberg | 23bb576 | 2010-12-21 23:01:27 +0200 | [diff] [blame] | 111 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | if (hdev->req_status == HCI_REQ_PEND) { |
| 114 | hdev->req_result = result; |
| 115 | hdev->req_status = HCI_REQ_DONE; |
| 116 | wake_up_interruptible(&hdev->req_wait_q); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void hci_req_cancel(struct hci_dev *hdev, int err) |
| 121 | { |
| 122 | BT_DBG("%s err 0x%2.2x", hdev->name, err); |
| 123 | |
| 124 | if (hdev->req_status == HCI_REQ_PEND) { |
| 125 | hdev->req_result = err; |
| 126 | hdev->req_status = HCI_REQ_CANCELED; |
| 127 | wake_up_interruptible(&hdev->req_wait_q); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Execute request and wait for completion. */ |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 132 | static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 133 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | DECLARE_WAITQUEUE(wait, current); |
| 136 | int err = 0; |
| 137 | |
| 138 | BT_DBG("%s start", hdev->name); |
| 139 | |
| 140 | hdev->req_status = HCI_REQ_PEND; |
| 141 | |
| 142 | add_wait_queue(&hdev->req_wait_q, &wait); |
| 143 | set_current_state(TASK_INTERRUPTIBLE); |
| 144 | |
| 145 | req(hdev, opt); |
| 146 | schedule_timeout(timeout); |
| 147 | |
| 148 | remove_wait_queue(&hdev->req_wait_q, &wait); |
| 149 | |
| 150 | if (signal_pending(current)) |
| 151 | return -EINTR; |
| 152 | |
| 153 | switch (hdev->req_status) { |
| 154 | case HCI_REQ_DONE: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 155 | err = -bt_err(hdev->req_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | break; |
| 157 | |
| 158 | case HCI_REQ_CANCELED: |
| 159 | err = -hdev->req_result; |
| 160 | break; |
| 161 | |
| 162 | default: |
| 163 | err = -ETIMEDOUT; |
| 164 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 167 | hdev->req_status = hdev->req_result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | BT_DBG("%s end: err %d", hdev->name, err); |
| 170 | |
| 171 | return err; |
| 172 | } |
| 173 | |
| 174 | static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 175 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | int ret; |
| 178 | |
Marcel Holtmann | 7c6a329 | 2008-09-12 03:11:54 +0200 | [diff] [blame] | 179 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 180 | return -ENETDOWN; |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* Serialize all requests */ |
| 183 | hci_req_lock(hdev); |
| 184 | ret = __hci_request(hdev, req, opt, timeout); |
| 185 | hci_req_unlock(hdev); |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | static void hci_reset_req(struct hci_dev *hdev, unsigned long opt) |
| 191 | { |
| 192 | BT_DBG("%s %ld", hdev->name, opt); |
| 193 | |
| 194 | /* Reset device */ |
Gustavo F. Padovan | f630cf0 | 2011-03-16 15:36:29 -0300 | [diff] [blame] | 195 | set_bit(HCI_RESET, &hdev->flags); |
Brian Gix | 6e4531c | 2011-10-28 16:12:08 -0700 | [diff] [blame] | 196 | memset(&hdev->features, 0, sizeof(hdev->features)); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 197 | hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Andrei Emeltchenko | e61ef49 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 200 | static void hci_init_req(struct hci_dev *hdev, unsigned long opt) |
| 201 | { |
Johan Hedberg | b0916ea | 2011-01-10 13:44:55 +0200 | [diff] [blame] | 202 | struct hci_cp_delete_stored_link_key cp; |
Andrei Emeltchenko | e61ef49 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 203 | struct sk_buff *skb; |
Marcel Holtmann | 1ebb925 | 2005-11-08 09:57:21 -0800 | [diff] [blame] | 204 | __le16 param; |
Marcel Holtmann | 89f2783 | 2007-09-09 08:39:49 +0200 | [diff] [blame] | 205 | __u8 flt_type; |
Andrei Emeltchenko | e61ef49 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 206 | |
| 207 | BT_DBG("%s %ld", hdev->name, opt); |
| 208 | |
| 209 | /* Driver initialization */ |
| 210 | |
| 211 | /* Special commands */ |
| 212 | while ((skb = skb_dequeue(&hdev->driver_init))) { |
| 213 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
| 214 | skb->dev = (void *) hdev; |
| 215 | |
| 216 | skb_queue_tail(&hdev->cmd_q, skb); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 217 | tasklet_schedule(&hdev->cmd_task); |
Andrei Emeltchenko | e61ef49 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 218 | } |
| 219 | skb_queue_purge(&hdev->driver_init); |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* Mandatory initialization */ |
Andrei Emeltchenko | e61ef49 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | /* Reset */ |
Gustavo F. Padovan | f630cf0 | 2011-03-16 15:36:29 -0300 | [diff] [blame] | 224 | if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) { |
| 225 | set_bit(HCI_RESET, &hdev->flags); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 226 | hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); |
Andrei Emeltchenko | e61ef49 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Marcel Holtmann | 1143e5a | 2006-09-23 09:57:20 +0200 | [diff] [blame] | 229 | /* Read Local Version */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 230 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Marcel Holtmann | 1143e5a | 2006-09-23 09:57:20 +0200 | [diff] [blame] | 231 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 232 | |
| 233 | /* Set default HCI Flow Control Mode */ |
| 234 | if (hdev->dev_type == HCI_BREDR) |
| 235 | hdev->flow_ctl_mode = HCI_PACKET_BASED_FLOW_CTL_MODE; |
| 236 | else |
| 237 | hdev->flow_ctl_mode = HCI_BLOCK_BASED_FLOW_CTL_MODE; |
| 238 | |
| 239 | /* Read HCI Flow Control Mode */ |
| 240 | hci_send_cmd(hdev, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL); |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 243 | hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 245 | /* Read Data Block Size (ACL mtu, max pkt, etc.) */ |
| 246 | hci_send_cmd(hdev, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | #if 0 |
| 249 | /* Host buffer size */ |
| 250 | { |
| 251 | struct hci_cp_host_buffer_size cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 252 | cp.acl_mtu = cpu_to_le16(HCI_MAX_ACL_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | cp.sco_mtu = HCI_MAX_SCO_SIZE; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 254 | cp.acl_max_pkt = cpu_to_le16(0xffff); |
| 255 | cp.sco_max_pkt = cpu_to_le16(0xffff); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 256 | hci_send_cmd(hdev, HCI_OP_HOST_BUFFER_SIZE, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | #endif |
| 259 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 260 | if (hdev->dev_type == HCI_BREDR) { |
| 261 | /* BR-EDR initialization */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 262 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 263 | /* Read Local Supported Features */ |
| 264 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 265 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 266 | /* Read BD Address */ |
| 267 | hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 269 | /* Read Class of Device */ |
| 270 | hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 272 | /* Read Local Name */ |
| 273 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 275 | /* Read Voice Setting */ |
| 276 | hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | /* Optional initialization */ |
| 279 | /* Clear Event Filters */ |
| 280 | flt_type = HCI_FLT_CLEAR_ALL; |
| 281 | hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type); |
Johan Hedberg | b0916ea | 2011-01-10 13:44:55 +0200 | [diff] [blame] | 282 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 283 | /* Connection accept timeout ~20 secs */ |
| 284 | param = cpu_to_le16(0x7d00); |
| 285 | hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
| 286 | |
| 287 | bacpy(&cp.bdaddr, BDADDR_ANY); |
| 288 | cp.delete_all = 1; |
| 289 | hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, |
| 290 | sizeof(cp), &cp); |
| 291 | } else { |
| 292 | /* AMP initialization */ |
| 293 | /* Connection accept timeout ~5 secs */ |
| 294 | param = cpu_to_le16(0x1f40); |
| 295 | hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
| 296 | |
| 297 | /* Read AMP Info */ |
| 298 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); |
| 299 | } |
Andrei Emeltchenko | e61ef49 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 302 | static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt) |
| 303 | { |
| 304 | BT_DBG("%s", hdev->name); |
| 305 | |
| 306 | /* Read LE buffer size */ |
| 307 | hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); |
Sunny Kapdi | 93bef89 | 2012-07-30 14:52:56 -0700 | [diff] [blame] | 308 | |
| 309 | /* Read LE clear white list */ |
| 310 | hci_send_cmd(hdev, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL); |
| 311 | |
| 312 | /* Read LE white list size */ |
| 313 | hci_send_cmd(hdev, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 314 | } |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | static void hci_scan_req(struct hci_dev *hdev, unsigned long opt) |
| 317 | { |
| 318 | __u8 scan = opt; |
| 319 | |
| 320 | BT_DBG("%s %x", hdev->name, scan); |
| 321 | |
| 322 | /* Inquiry and Page scans */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 323 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void hci_auth_req(struct hci_dev *hdev, unsigned long opt) |
| 327 | { |
| 328 | __u8 auth = opt; |
| 329 | |
| 330 | BT_DBG("%s %x", hdev->name, auth); |
| 331 | |
| 332 | /* Authentication */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 333 | hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt) |
| 337 | { |
| 338 | __u8 encrypt = opt; |
| 339 | |
| 340 | BT_DBG("%s %x", hdev->name, encrypt); |
| 341 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 342 | /* Encryption */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 343 | hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 346 | static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt) |
| 347 | { |
| 348 | __le16 policy = cpu_to_le16(opt); |
| 349 | |
Marcel Holtmann | a418b89 | 2008-11-30 12:17:28 +0100 | [diff] [blame] | 350 | BT_DBG("%s %x", hdev->name, policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 351 | |
| 352 | /* Default link policy */ |
| 353 | hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); |
| 354 | } |
| 355 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 356 | /* Get HCI device by index. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | * Device is held on return. */ |
| 358 | struct hci_dev *hci_dev_get(int index) |
| 359 | { |
| 360 | struct hci_dev *hdev = NULL; |
| 361 | struct list_head *p; |
| 362 | |
| 363 | BT_DBG("%d", index); |
| 364 | |
| 365 | if (index < 0) |
| 366 | return NULL; |
| 367 | |
| 368 | read_lock(&hci_dev_list_lock); |
| 369 | list_for_each(p, &hci_dev_list) { |
| 370 | struct hci_dev *d = list_entry(p, struct hci_dev, list); |
| 371 | if (d->id == index) { |
| 372 | hdev = hci_dev_hold(d); |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | read_unlock(&hci_dev_list_lock); |
| 377 | return hdev; |
| 378 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 379 | EXPORT_SYMBOL(hci_dev_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | /* ---- Inquiry support ---- */ |
| 382 | static void inquiry_cache_flush(struct hci_dev *hdev) |
| 383 | { |
| 384 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 385 | struct inquiry_entry *next = cache->list, *e; |
| 386 | |
| 387 | BT_DBG("cache %p", cache); |
| 388 | |
| 389 | cache->list = NULL; |
| 390 | while ((e = next)) { |
| 391 | next = e->next; |
| 392 | kfree(e); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 397 | { |
| 398 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 399 | struct inquiry_entry *e; |
| 400 | |
| 401 | BT_DBG("cache %p, %s", cache, batostr(bdaddr)); |
| 402 | |
| 403 | for (e = cache->list; e; e = e->next) |
| 404 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 405 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return e; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data) |
| 410 | { |
| 411 | struct inquiry_cache *cache = &hdev->inq_cache; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 412 | struct inquiry_entry *ie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr)); |
| 415 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 416 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
| 417 | if (!ie) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /* Entry not in the cache. Add new one. */ |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 419 | ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); |
| 420 | if (!ie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return; |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 422 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 423 | ie->next = cache->list; |
| 424 | cache->list = ie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 427 | memcpy(&ie->data, data, sizeof(*data)); |
| 428 | ie->timestamp = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | cache->timestamp = jiffies; |
| 430 | } |
| 431 | |
| 432 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) |
| 433 | { |
| 434 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 435 | struct inquiry_info *info = (struct inquiry_info *) buf; |
| 436 | struct inquiry_entry *e; |
| 437 | int copied = 0; |
| 438 | |
| 439 | for (e = cache->list; e && copied < num; e = e->next, copied++) { |
| 440 | struct inquiry_data *data = &e->data; |
| 441 | bacpy(&info->bdaddr, &data->bdaddr); |
| 442 | info->pscan_rep_mode = data->pscan_rep_mode; |
| 443 | info->pscan_period_mode = data->pscan_period_mode; |
| 444 | info->pscan_mode = data->pscan_mode; |
| 445 | memcpy(info->dev_class, data->dev_class, 3); |
| 446 | info->clock_offset = data->clock_offset; |
| 447 | info++; |
| 448 | } |
| 449 | |
| 450 | BT_DBG("cache %p, copied %d", cache, copied); |
| 451 | return copied; |
| 452 | } |
| 453 | |
| 454 | static void hci_inq_req(struct hci_dev *hdev, unsigned long opt) |
| 455 | { |
| 456 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; |
| 457 | struct hci_cp_inquiry cp; |
| 458 | |
| 459 | BT_DBG("%s", hdev->name); |
| 460 | |
| 461 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 462 | return; |
| 463 | |
| 464 | /* Start Inquiry */ |
| 465 | memcpy(&cp.lap, &ir->lap, 3); |
| 466 | cp.length = ir->length; |
| 467 | cp.num_rsp = ir->num_rsp; |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 468 | hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | int hci_inquiry(void __user *arg) |
| 472 | { |
| 473 | __u8 __user *ptr = arg; |
| 474 | struct hci_inquiry_req ir; |
| 475 | struct hci_dev *hdev; |
| 476 | int err = 0, do_inquiry = 0, max_rsp; |
| 477 | long timeo; |
| 478 | __u8 *buf; |
| 479 | |
| 480 | if (copy_from_user(&ir, ptr, sizeof(ir))) |
| 481 | return -EFAULT; |
| 482 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 483 | hdev = hci_dev_get(ir.dev_id); |
| 484 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return -ENODEV; |
| 486 | |
| 487 | hci_dev_lock_bh(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 488 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 489 | inquiry_cache_empty(hdev) || |
| 490 | ir.flags & IREQ_CACHE_FLUSH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | inquiry_cache_flush(hdev); |
| 492 | do_inquiry = 1; |
| 493 | } |
| 494 | hci_dev_unlock_bh(hdev); |
| 495 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 496 | timeo = ir.length * msecs_to_jiffies(2000); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 497 | |
| 498 | if (do_inquiry) { |
| 499 | err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo); |
| 500 | if (err < 0) |
| 501 | goto done; |
| 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | /* for unlimited number of responses we will use buffer with 255 entries */ |
| 505 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; |
| 506 | |
| 507 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then |
| 508 | * copy it to the user space. |
| 509 | */ |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 510 | buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 511 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | err = -ENOMEM; |
| 513 | goto done; |
| 514 | } |
| 515 | |
| 516 | hci_dev_lock_bh(hdev); |
| 517 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); |
| 518 | hci_dev_unlock_bh(hdev); |
| 519 | |
| 520 | BT_DBG("num_rsp %d", ir.num_rsp); |
| 521 | |
| 522 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { |
| 523 | ptr += sizeof(ir); |
| 524 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * |
| 525 | ir.num_rsp)) |
| 526 | err = -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 527 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | err = -EFAULT; |
| 529 | |
| 530 | kfree(buf); |
| 531 | |
| 532 | done: |
| 533 | hci_dev_put(hdev); |
| 534 | return err; |
| 535 | } |
| 536 | |
| 537 | /* ---- HCI ioctl helpers ---- */ |
| 538 | |
| 539 | int hci_dev_open(__u16 dev) |
| 540 | { |
| 541 | struct hci_dev *hdev; |
| 542 | int ret = 0; |
| 543 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 544 | hdev = hci_dev_get(dev); |
| 545 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | return -ENODEV; |
| 547 | |
| 548 | BT_DBG("%s %p", hdev->name, hdev); |
| 549 | |
| 550 | hci_req_lock(hdev); |
| 551 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 552 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) { |
| 553 | ret = -ERFKILL; |
| 554 | goto done; |
| 555 | } |
| 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 558 | ret = -EALREADY; |
| 559 | goto done; |
| 560 | } |
| 561 | |
| 562 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 563 | set_bit(HCI_RAW, &hdev->flags); |
| 564 | |
| 565 | if (hdev->open(hdev)) { |
| 566 | ret = -EIO; |
| 567 | goto done; |
| 568 | } |
| 569 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 570 | if (!skb_queue_empty(&hdev->cmd_q)) { |
| 571 | BT_ERR("command queue is not empty, purging"); |
| 572 | skb_queue_purge(&hdev->cmd_q); |
| 573 | } |
| 574 | if (!skb_queue_empty(&hdev->rx_q)) { |
| 575 | BT_ERR("rx queue is not empty, purging"); |
| 576 | skb_queue_purge(&hdev->rx_q); |
| 577 | } |
| 578 | if (!skb_queue_empty(&hdev->raw_q)) { |
| 579 | BT_ERR("raw queue is not empty, purging"); |
| 580 | skb_queue_purge(&hdev->raw_q); |
| 581 | } |
| 582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 584 | atomic_set(&hdev->cmd_cnt, 1); |
| 585 | set_bit(HCI_INIT, &hdev->flags); |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 586 | hdev->init_last_cmd = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 588 | ret = __hci_request(hdev, hci_init_req, 0, |
| 589 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 591 | if (lmp_le_capable(hdev)) |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 592 | ret = __hci_request(hdev, hci_le_init_req, 0, |
| 593 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
| 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | clear_bit(HCI_INIT, &hdev->flags); |
| 596 | } |
| 597 | |
| 598 | if (!ret) { |
| 599 | hci_dev_hold(hdev); |
| 600 | set_bit(HCI_UP, &hdev->flags); |
| 601 | hci_notify(hdev, HCI_DEV_UP); |
Peter Krystad | 1fc4407 | 2011-08-30 15:38:12 -0700 | [diff] [blame] | 602 | if (!test_bit(HCI_SETUP, &hdev->flags) && |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 603 | hdev->dev_type == HCI_BREDR) { |
| 604 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 605 | mgmt_powered(hdev->id, 1); |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 606 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 607 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 608 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | /* Init failed, cleanup */ |
| 610 | tasklet_kill(&hdev->rx_task); |
| 611 | tasklet_kill(&hdev->tx_task); |
| 612 | tasklet_kill(&hdev->cmd_task); |
| 613 | |
| 614 | skb_queue_purge(&hdev->cmd_q); |
| 615 | skb_queue_purge(&hdev->rx_q); |
| 616 | |
| 617 | if (hdev->flush) |
| 618 | hdev->flush(hdev); |
| 619 | |
| 620 | if (hdev->sent_cmd) { |
| 621 | kfree_skb(hdev->sent_cmd); |
| 622 | hdev->sent_cmd = NULL; |
| 623 | } |
| 624 | |
| 625 | hdev->close(hdev); |
| 626 | hdev->flags = 0; |
| 627 | } |
| 628 | |
| 629 | done: |
| 630 | hci_req_unlock(hdev); |
| 631 | hci_dev_put(hdev); |
| 632 | return ret; |
| 633 | } |
| 634 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 635 | static int hci_dev_do_close(struct hci_dev *hdev, u8 is_process) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { |
Mat Martineau | 4106b99 | 2011-11-18 15:26:21 -0800 | [diff] [blame] | 637 | unsigned long keepflags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | BT_DBG("%s %p", hdev->name, hdev); |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | hci_req_cancel(hdev, ENODEV); |
| 642 | hci_req_lock(hdev); |
| 643 | |
| 644 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 645 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | hci_req_unlock(hdev); |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* Kill RX and TX tasks */ |
| 651 | tasklet_kill(&hdev->rx_task); |
| 652 | tasklet_kill(&hdev->tx_task); |
| 653 | |
| 654 | hci_dev_lock_bh(hdev); |
| 655 | inquiry_cache_flush(hdev); |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 656 | hci_conn_hash_flush(hdev, is_process); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | hci_dev_unlock_bh(hdev); |
| 658 | |
| 659 | hci_notify(hdev, HCI_DEV_DOWN); |
| 660 | |
Bhasker Neti | ffdff57 | 2011-12-21 17:24:01 -0800 | [diff] [blame] | 661 | if (hdev->dev_type == HCI_BREDR) { |
| 662 | hci_dev_lock_bh(hdev); |
| 663 | mgmt_powered(hdev->id, 0); |
| 664 | hci_dev_unlock_bh(hdev); |
| 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | if (hdev->flush) |
| 668 | hdev->flush(hdev); |
| 669 | |
| 670 | /* Reset device */ |
| 671 | skb_queue_purge(&hdev->cmd_q); |
| 672 | atomic_set(&hdev->cmd_cnt, 1); |
| 673 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 674 | set_bit(HCI_INIT, &hdev->flags); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 675 | __hci_request(hdev, hci_reset_req, 0, |
Gustavo F. Padovan | cad44c2 | 2011-12-23 18:59:13 -0200 | [diff] [blame] | 676 | msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | clear_bit(HCI_INIT, &hdev->flags); |
| 678 | } |
| 679 | |
| 680 | /* Kill cmd task */ |
| 681 | tasklet_kill(&hdev->cmd_task); |
| 682 | |
| 683 | /* Drop queues */ |
| 684 | skb_queue_purge(&hdev->rx_q); |
| 685 | skb_queue_purge(&hdev->cmd_q); |
| 686 | skb_queue_purge(&hdev->raw_q); |
| 687 | |
| 688 | /* Drop last sent command */ |
| 689 | if (hdev->sent_cmd) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 690 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | kfree_skb(hdev->sent_cmd); |
| 692 | hdev->sent_cmd = NULL; |
| 693 | } |
| 694 | |
| 695 | /* After this point our queues are empty |
| 696 | * and no tasks are scheduled. */ |
| 697 | hdev->close(hdev); |
| 698 | |
Mat Martineau | 4106b99 | 2011-11-18 15:26:21 -0800 | [diff] [blame] | 699 | /* Clear only non-persistent flags */ |
| 700 | if (test_bit(HCI_MGMT, &hdev->flags)) |
| 701 | set_bit(HCI_MGMT, &keepflags); |
| 702 | if (test_bit(HCI_LINK_KEYS, &hdev->flags)) |
| 703 | set_bit(HCI_LINK_KEYS, &keepflags); |
| 704 | if (test_bit(HCI_DEBUG_KEYS, &hdev->flags)) |
| 705 | set_bit(HCI_DEBUG_KEYS, &keepflags); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 706 | |
Mat Martineau | 4106b99 | 2011-11-18 15:26:21 -0800 | [diff] [blame] | 707 | hdev->flags = keepflags; |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | hci_req_unlock(hdev); |
| 710 | |
| 711 | hci_dev_put(hdev); |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | int hci_dev_close(__u16 dev) |
| 716 | { |
| 717 | struct hci_dev *hdev; |
| 718 | int err; |
| 719 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 720 | hdev = hci_dev_get(dev); |
| 721 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | return -ENODEV; |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 723 | err = hci_dev_do_close(hdev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | hci_dev_put(hdev); |
| 725 | return err; |
| 726 | } |
| 727 | |
| 728 | int hci_dev_reset(__u16 dev) |
| 729 | { |
| 730 | struct hci_dev *hdev; |
| 731 | int ret = 0; |
| 732 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 733 | hdev = hci_dev_get(dev); |
| 734 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return -ENODEV; |
| 736 | |
| 737 | hci_req_lock(hdev); |
| 738 | tasklet_disable(&hdev->tx_task); |
| 739 | |
| 740 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 741 | goto done; |
| 742 | |
| 743 | /* Drop queues */ |
| 744 | skb_queue_purge(&hdev->rx_q); |
| 745 | skb_queue_purge(&hdev->cmd_q); |
| 746 | |
| 747 | hci_dev_lock_bh(hdev); |
| 748 | inquiry_cache_flush(hdev); |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 749 | hci_conn_hash_flush(hdev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | hci_dev_unlock_bh(hdev); |
| 751 | |
| 752 | if (hdev->flush) |
| 753 | hdev->flush(hdev); |
| 754 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 755 | atomic_set(&hdev->cmd_cnt, 1); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 756 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | if (!test_bit(HCI_RAW, &hdev->flags)) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 759 | ret = __hci_request(hdev, hci_reset_req, 0, |
| 760 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
| 762 | done: |
| 763 | tasklet_enable(&hdev->tx_task); |
| 764 | hci_req_unlock(hdev); |
| 765 | hci_dev_put(hdev); |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | int hci_dev_reset_stat(__u16 dev) |
| 770 | { |
| 771 | struct hci_dev *hdev; |
| 772 | int ret = 0; |
| 773 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 774 | hdev = hci_dev_get(dev); |
| 775 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | return -ENODEV; |
| 777 | |
| 778 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 779 | |
| 780 | hci_dev_put(hdev); |
| 781 | |
| 782 | return ret; |
| 783 | } |
| 784 | |
| 785 | int hci_dev_cmd(unsigned int cmd, void __user *arg) |
| 786 | { |
| 787 | struct hci_dev *hdev; |
| 788 | struct hci_dev_req dr; |
| 789 | int err = 0; |
| 790 | |
| 791 | if (copy_from_user(&dr, arg, sizeof(dr))) |
| 792 | return -EFAULT; |
| 793 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 794 | hdev = hci_dev_get(dr.dev_id); |
| 795 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | return -ENODEV; |
| 797 | |
| 798 | switch (cmd) { |
| 799 | case HCISETAUTH: |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 800 | err = hci_request(hdev, hci_auth_req, dr.dev_opt, |
| 801 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | break; |
| 803 | |
| 804 | case HCISETENCRYPT: |
| 805 | if (!lmp_encrypt_capable(hdev)) { |
| 806 | err = -EOPNOTSUPP; |
| 807 | break; |
| 808 | } |
| 809 | |
| 810 | if (!test_bit(HCI_AUTH, &hdev->flags)) { |
| 811 | /* Auth must be enabled first */ |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 812 | err = hci_request(hdev, hci_auth_req, dr.dev_opt, |
| 813 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | if (err) |
| 815 | break; |
| 816 | } |
| 817 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 818 | err = hci_request(hdev, hci_encrypt_req, dr.dev_opt, |
| 819 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | break; |
| 821 | |
| 822 | case HCISETSCAN: |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 823 | err = hci_request(hdev, hci_scan_req, dr.dev_opt, |
| 824 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | break; |
| 826 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 827 | case HCISETLINKPOL: |
| 828 | err = hci_request(hdev, hci_linkpol_req, dr.dev_opt, |
| 829 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
| 830 | break; |
| 831 | |
| 832 | case HCISETLINKMODE: |
| 833 | hdev->link_mode = ((__u16) dr.dev_opt) & |
| 834 | (HCI_LM_MASTER | HCI_LM_ACCEPT); |
| 835 | break; |
| 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | case HCISETPTYPE: |
| 838 | hdev->pkt_type = (__u16) dr.dev_opt; |
| 839 | break; |
| 840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | case HCISETACLMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 842 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 843 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | break; |
| 845 | |
| 846 | case HCISETSCOMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 847 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 848 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | break; |
| 850 | |
| 851 | default: |
| 852 | err = -EINVAL; |
| 853 | break; |
| 854 | } |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | hci_dev_put(hdev); |
| 857 | return err; |
| 858 | } |
| 859 | |
| 860 | int hci_get_dev_list(void __user *arg) |
| 861 | { |
| 862 | struct hci_dev_list_req *dl; |
| 863 | struct hci_dev_req *dr; |
| 864 | struct list_head *p; |
| 865 | int n = 0, size, err; |
| 866 | __u16 dev_num; |
| 867 | |
| 868 | if (get_user(dev_num, (__u16 __user *) arg)) |
| 869 | return -EFAULT; |
| 870 | |
| 871 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) |
| 872 | return -EINVAL; |
| 873 | |
| 874 | size = sizeof(*dl) + dev_num * sizeof(*dr); |
| 875 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 876 | dl = kzalloc(size, GFP_KERNEL); |
| 877 | if (!dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | return -ENOMEM; |
| 879 | |
| 880 | dr = dl->dev_req; |
| 881 | |
| 882 | read_lock_bh(&hci_dev_list_lock); |
| 883 | list_for_each(p, &hci_dev_list) { |
| 884 | struct hci_dev *hdev; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | hdev = list_entry(p, struct hci_dev, list); |
| 887 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 888 | hci_del_off_timer(hdev); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 889 | |
| 890 | if (!test_bit(HCI_MGMT, &hdev->flags)) |
| 891 | set_bit(HCI_PAIRABLE, &hdev->flags); |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | (dr + n)->dev_id = hdev->id; |
| 894 | (dr + n)->dev_opt = hdev->flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | if (++n >= dev_num) |
| 897 | break; |
| 898 | } |
| 899 | read_unlock_bh(&hci_dev_list_lock); |
| 900 | |
| 901 | dl->dev_num = n; |
| 902 | size = sizeof(*dl) + n * sizeof(*dr); |
| 903 | |
| 904 | err = copy_to_user(arg, dl, size); |
| 905 | kfree(dl); |
| 906 | |
| 907 | return err ? -EFAULT : 0; |
| 908 | } |
| 909 | |
| 910 | int hci_get_dev_info(void __user *arg) |
| 911 | { |
| 912 | struct hci_dev *hdev; |
| 913 | struct hci_dev_info di; |
| 914 | int err = 0; |
| 915 | |
| 916 | if (copy_from_user(&di, arg, sizeof(di))) |
| 917 | return -EFAULT; |
| 918 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 919 | hdev = hci_dev_get(di.dev_id); |
| 920 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return -ENODEV; |
| 922 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 923 | hci_del_off_timer(hdev); |
| 924 | |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 925 | if (!test_bit(HCI_MGMT, &hdev->flags)) |
| 926 | set_bit(HCI_PAIRABLE, &hdev->flags); |
| 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | strcpy(di.name, hdev->name); |
| 929 | di.bdaddr = hdev->bdaddr; |
Marcel Holtmann | 943da25 | 2010-02-13 02:28:41 +0100 | [diff] [blame] | 930 | di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | di.flags = hdev->flags; |
| 932 | di.pkt_type = hdev->pkt_type; |
| 933 | di.acl_mtu = hdev->acl_mtu; |
| 934 | di.acl_pkts = hdev->acl_pkts; |
| 935 | di.sco_mtu = hdev->sco_mtu; |
| 936 | di.sco_pkts = hdev->sco_pkts; |
| 937 | di.link_policy = hdev->link_policy; |
| 938 | di.link_mode = hdev->link_mode; |
| 939 | |
| 940 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); |
| 941 | memcpy(&di.features, &hdev->features, sizeof(di.features)); |
| 942 | |
| 943 | if (copy_to_user(arg, &di, sizeof(di))) |
| 944 | err = -EFAULT; |
| 945 | |
| 946 | hci_dev_put(hdev); |
| 947 | |
| 948 | return err; |
| 949 | } |
| 950 | |
| 951 | /* ---- Interface to HCI drivers ---- */ |
| 952 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 953 | static int hci_rfkill_set_block(void *data, bool blocked) |
| 954 | { |
| 955 | struct hci_dev *hdev = data; |
| 956 | |
| 957 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); |
| 958 | |
| 959 | if (!blocked) |
| 960 | return 0; |
| 961 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 962 | hci_dev_do_close(hdev, 0); |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static const struct rfkill_ops hci_rfkill_ops = { |
| 968 | .set_block = hci_rfkill_set_block, |
| 969 | }; |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | /* Alloc HCI device */ |
| 972 | struct hci_dev *hci_alloc_dev(void) |
| 973 | { |
| 974 | struct hci_dev *hdev; |
| 975 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 976 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | if (!hdev) |
| 978 | return NULL; |
| 979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | skb_queue_head_init(&hdev->driver_init); |
| 981 | |
| 982 | return hdev; |
| 983 | } |
| 984 | EXPORT_SYMBOL(hci_alloc_dev); |
| 985 | |
| 986 | /* Free HCI device */ |
| 987 | void hci_free_dev(struct hci_dev *hdev) |
| 988 | { |
| 989 | skb_queue_purge(&hdev->driver_init); |
| 990 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 991 | /* will free via device release */ |
| 992 | put_device(&hdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } |
| 994 | EXPORT_SYMBOL(hci_free_dev); |
| 995 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 996 | static void hci_power_on(struct work_struct *work) |
| 997 | { |
| 998 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); |
Inga Stotland | 5029fc2 | 2011-09-12 15:22:52 -0700 | [diff] [blame] | 999 | int err; |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1000 | |
| 1001 | BT_DBG("%s", hdev->name); |
| 1002 | |
Inga Stotland | 5029fc2 | 2011-09-12 15:22:52 -0700 | [diff] [blame] | 1003 | err = hci_dev_open(hdev->id); |
| 1004 | if (err && err != -EALREADY) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1005 | return; |
| 1006 | |
Peter Krystad | 1fc4407 | 2011-08-30 15:38:12 -0700 | [diff] [blame] | 1007 | if (test_bit(HCI_AUTO_OFF, &hdev->flags) && |
| 1008 | hdev->dev_type == HCI_BREDR) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1009 | mod_timer(&hdev->off_timer, |
| 1010 | jiffies + msecs_to_jiffies(AUTO_OFF_TIMEOUT)); |
| 1011 | |
Peter Krystad | 1fc4407 | 2011-08-30 15:38:12 -0700 | [diff] [blame] | 1012 | if (test_and_clear_bit(HCI_SETUP, &hdev->flags) && |
| 1013 | hdev->dev_type == HCI_BREDR) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1014 | mgmt_index_added(hdev->id); |
| 1015 | } |
| 1016 | |
| 1017 | static void hci_power_off(struct work_struct *work) |
| 1018 | { |
| 1019 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_off); |
| 1020 | |
| 1021 | BT_DBG("%s", hdev->name); |
| 1022 | |
| 1023 | hci_dev_close(hdev->id); |
| 1024 | } |
| 1025 | |
| 1026 | static void hci_auto_off(unsigned long data) |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1027 | { |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1028 | struct hci_dev *hdev = (struct hci_dev *) data; |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1029 | |
| 1030 | BT_DBG("%s", hdev->name); |
| 1031 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1032 | clear_bit(HCI_AUTO_OFF, &hdev->flags); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1033 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1034 | queue_work(hdev->workqueue, &hdev->power_off); |
| 1035 | } |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1036 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1037 | void hci_del_off_timer(struct hci_dev *hdev) |
| 1038 | { |
| 1039 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1040 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1041 | clear_bit(HCI_AUTO_OFF, &hdev->flags); |
| 1042 | del_timer(&hdev->off_timer); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1045 | int hci_uuids_clear(struct hci_dev *hdev) |
| 1046 | { |
| 1047 | struct list_head *p, *n; |
| 1048 | |
| 1049 | list_for_each_safe(p, n, &hdev->uuids) { |
| 1050 | struct bt_uuid *uuid; |
| 1051 | |
| 1052 | uuid = list_entry(p, struct bt_uuid, list); |
| 1053 | |
| 1054 | list_del(p); |
| 1055 | kfree(uuid); |
| 1056 | } |
| 1057 | |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1061 | int hci_link_keys_clear(struct hci_dev *hdev) |
| 1062 | { |
| 1063 | struct list_head *p, *n; |
| 1064 | |
| 1065 | list_for_each_safe(p, n, &hdev->link_keys) { |
| 1066 | struct link_key *key; |
| 1067 | |
| 1068 | key = list_entry(p, struct link_key, list); |
| 1069 | |
| 1070 | list_del(p); |
| 1071 | kfree(key); |
| 1072 | } |
| 1073 | |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1078 | { |
| 1079 | struct list_head *p; |
| 1080 | |
| 1081 | list_for_each(p, &hdev->link_keys) { |
| 1082 | struct link_key *k; |
| 1083 | |
| 1084 | k = list_entry(p, struct link_key, list); |
| 1085 | |
| 1086 | if (bacmp(bdaddr, &k->bdaddr) == 0) |
| 1087 | return k; |
| 1088 | } |
| 1089 | |
| 1090 | return NULL; |
| 1091 | } |
| 1092 | |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1093 | struct link_key *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]) |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1094 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1095 | struct list_head *p; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1096 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1097 | list_for_each(p, &hdev->link_keys) { |
| 1098 | struct link_key *k; |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1099 | struct key_master_id *id; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1100 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1101 | k = list_entry(p, struct link_key, list); |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1102 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1103 | if (k->key_type != KEY_TYPE_LTK) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1104 | continue; |
| 1105 | |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1106 | if (k->dlen != sizeof(*id)) |
| 1107 | continue; |
| 1108 | |
| 1109 | id = (void *) &k->data; |
| 1110 | if (id->ediv == ediv && |
| 1111 | (memcmp(rand, id->rand, sizeof(id->rand)) == 0)) |
| 1112 | return k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | EXPORT_SYMBOL(hci_find_ltk); |
| 1118 | |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1119 | struct link_key *hci_find_link_key_type(struct hci_dev *hdev, |
| 1120 | bdaddr_t *bdaddr, u8 type) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1121 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1122 | struct list_head *p; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1123 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1124 | list_for_each(p, &hdev->link_keys) { |
| 1125 | struct link_key *k; |
| 1126 | |
| 1127 | k = list_entry(p, struct link_key, list); |
| 1128 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1129 | if ((k->key_type == type) && (bacmp(bdaddr, &k->bdaddr) == 0)) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1130 | return k; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1131 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1132 | |
| 1133 | return NULL; |
| 1134 | } |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1135 | EXPORT_SYMBOL(hci_find_link_key_type); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1136 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1137 | int hci_add_link_key(struct hci_dev *hdev, int new_key, bdaddr_t *bdaddr, |
| 1138 | u8 *val, u8 type, u8 pin_len) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1139 | { |
| 1140 | struct link_key *key, *old_key; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1141 | struct hci_conn *conn; |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1142 | u8 old_key_type; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1143 | u8 bonded = 0; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1144 | |
| 1145 | old_key = hci_find_link_key(hdev, bdaddr); |
| 1146 | if (old_key) { |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1147 | old_key_type = old_key->key_type; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1148 | key = old_key; |
| 1149 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1150 | old_key_type = 0xff; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1151 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
| 1152 | if (!key) |
| 1153 | return -ENOMEM; |
| 1154 | list_add(&key->list, &hdev->link_keys); |
| 1155 | } |
| 1156 | |
| 1157 | BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type); |
| 1158 | |
| 1159 | bacpy(&key->bdaddr, bdaddr); |
| 1160 | memcpy(key->val, val, 16); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1161 | key->auth = 0x01; |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1162 | key->key_type = type; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1163 | key->pin_len = pin_len; |
| 1164 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1165 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr); |
Srinivas Krovvidi | 9ff5145 | 2011-09-27 19:25:02 +0530 | [diff] [blame] | 1166 | /* Store the link key persistently if one of the following is true: |
| 1167 | * 1. the remote side is using dedicated bonding since in that case |
| 1168 | * also the local requirements are set to dedicated bonding |
| 1169 | * 2. the local side had dedicated bonding as a requirement |
| 1170 | * 3. this is a legacy link key |
| 1171 | * 4. this is a changed combination key and there was a previously |
| 1172 | * stored one |
| 1173 | * If none of the above match only keep the link key around for |
| 1174 | * this connection and set the temporary flag for the device. |
| 1175 | */ |
Johan Hedberg | 4748fed | 2011-04-28 11:29:02 -0700 | [diff] [blame] | 1176 | |
Brian Gix | dfdd936 | 2011-08-18 09:58:02 -0700 | [diff] [blame] | 1177 | if (conn) { |
Srinivas Krovvidi | 9ff5145 | 2011-09-27 19:25:02 +0530 | [diff] [blame] | 1178 | if ((conn->remote_auth > 0x01) || |
| 1179 | (conn->auth_initiator && conn->auth_type > 0x01) || |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1180 | (key->key_type < 0x03) || |
| 1181 | (key->key_type == 0x06 && old_key_type != 0xff)) |
Brian Gix | dfdd936 | 2011-08-18 09:58:02 -0700 | [diff] [blame] | 1182 | bonded = 1; |
| 1183 | } |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1184 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1185 | if (new_key) |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1186 | mgmt_new_key(hdev->id, key, bonded); |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1187 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1188 | if (type == 0x06) |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1189 | key->key_type = old_key_type; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1194 | int hci_add_ltk(struct hci_dev *hdev, int new_key, bdaddr_t *bdaddr, |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1195 | u8 addr_type, u8 key_size, u8 auth, |
| 1196 | __le16 ediv, u8 rand[8], u8 ltk[16]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1197 | { |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1198 | struct link_key *key, *old_key; |
| 1199 | struct key_master_id *id; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1200 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1201 | BT_DBG("%s Auth: %2.2X addr %s type: %d", hdev->name, auth, |
| 1202 | batostr(bdaddr), addr_type); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1203 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1204 | old_key = hci_find_link_key_type(hdev, bdaddr, KEY_TYPE_LTK); |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1205 | if (old_key) { |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1206 | key = old_key; |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1207 | } else { |
| 1208 | key = kzalloc(sizeof(*key) + sizeof(*id), GFP_ATOMIC); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1209 | if (!key) |
| 1210 | return -ENOMEM; |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1211 | list_add(&key->list, &hdev->link_keys); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1212 | } |
| 1213 | |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1214 | key->dlen = sizeof(*id); |
| 1215 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1216 | bacpy(&key->bdaddr, bdaddr); |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1217 | key->addr_type = addr_type; |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1218 | memcpy(key->val, ltk, sizeof(key->val)); |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1219 | key->key_type = KEY_TYPE_LTK; |
Vinicius Costa Gomes | 1fa2de3 | 2011-07-08 18:31:45 -0300 | [diff] [blame] | 1220 | key->pin_len = key_size; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1221 | key->auth = auth; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1222 | |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1223 | id = (void *) &key->data; |
| 1224 | id->ediv = ediv; |
| 1225 | memcpy(id->rand, rand, sizeof(id->rand)); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1226 | |
Vinicius Costa Gomes | 7f9004d | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1227 | if (new_key) |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1228 | mgmt_new_key(hdev->id, key, auth & 0x01); |
Vinicius Costa Gomes | 261cc5a | 2012-02-02 21:08:05 -0300 | [diff] [blame] | 1229 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1230 | return 0; |
| 1231 | } |
| 1232 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1233 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1234 | { |
| 1235 | struct link_key *key; |
| 1236 | |
| 1237 | key = hci_find_link_key(hdev, bdaddr); |
| 1238 | if (!key) |
| 1239 | return -ENOENT; |
| 1240 | |
| 1241 | BT_DBG("%s removing %s", hdev->name, batostr(bdaddr)); |
| 1242 | |
| 1243 | list_del(&key->list); |
| 1244 | kfree(key); |
| 1245 | |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1249 | /* HCI command timer function */ |
| 1250 | static void hci_cmd_timer(unsigned long arg) |
| 1251 | { |
| 1252 | struct hci_dev *hdev = (void *) arg; |
| 1253 | |
| 1254 | BT_ERR("%s command tx timeout", hdev->name); |
| 1255 | atomic_set(&hdev->cmd_cnt, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1256 | clear_bit(HCI_RESET, &hdev->flags); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1257 | tasklet_schedule(&hdev->cmd_task); |
| 1258 | } |
| 1259 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1260 | struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, |
| 1261 | bdaddr_t *bdaddr) |
| 1262 | { |
| 1263 | struct oob_data *data; |
| 1264 | |
| 1265 | list_for_each_entry(data, &hdev->remote_oob_data, list) |
| 1266 | if (bacmp(bdaddr, &data->bdaddr) == 0) |
| 1267 | return data; |
| 1268 | |
| 1269 | return NULL; |
| 1270 | } |
| 1271 | |
| 1272 | int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1273 | { |
| 1274 | struct oob_data *data; |
| 1275 | |
| 1276 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 1277 | if (!data) |
| 1278 | return -ENOENT; |
| 1279 | |
| 1280 | BT_DBG("%s removing %s", hdev->name, batostr(bdaddr)); |
| 1281 | |
| 1282 | list_del(&data->list); |
| 1283 | kfree(data); |
| 1284 | |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | int hci_remote_oob_data_clear(struct hci_dev *hdev) |
| 1289 | { |
| 1290 | struct oob_data *data, *n; |
| 1291 | |
| 1292 | list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { |
| 1293 | list_del(&data->list); |
| 1294 | kfree(data); |
| 1295 | } |
| 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1300 | static void hci_adv_clear(unsigned long arg) |
| 1301 | { |
| 1302 | struct hci_dev *hdev = (void *) arg; |
| 1303 | |
| 1304 | hci_adv_entries_clear(hdev); |
| 1305 | } |
| 1306 | |
| 1307 | int hci_adv_entries_clear(struct hci_dev *hdev) |
| 1308 | { |
| 1309 | struct list_head *p, *n; |
| 1310 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1311 | BT_DBG(""); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1312 | write_lock_bh(&hdev->adv_entries_lock); |
| 1313 | |
| 1314 | list_for_each_safe(p, n, &hdev->adv_entries) { |
| 1315 | struct adv_entry *entry; |
| 1316 | |
| 1317 | entry = list_entry(p, struct adv_entry, list); |
| 1318 | |
| 1319 | list_del(p); |
| 1320 | kfree(entry); |
| 1321 | } |
| 1322 | |
| 1323 | write_unlock_bh(&hdev->adv_entries_lock); |
| 1324 | |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | struct adv_entry *hci_find_adv_entry(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1329 | { |
| 1330 | struct list_head *p; |
| 1331 | struct adv_entry *res = NULL; |
| 1332 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1333 | BT_DBG(""); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1334 | read_lock_bh(&hdev->adv_entries_lock); |
| 1335 | |
| 1336 | list_for_each(p, &hdev->adv_entries) { |
| 1337 | struct adv_entry *entry; |
| 1338 | |
| 1339 | entry = list_entry(p, struct adv_entry, list); |
| 1340 | |
| 1341 | if (bacmp(bdaddr, &entry->bdaddr) == 0) { |
| 1342 | res = entry; |
| 1343 | goto out; |
| 1344 | } |
| 1345 | } |
| 1346 | out: |
| 1347 | read_unlock_bh(&hdev->adv_entries_lock); |
| 1348 | return res; |
| 1349 | } |
| 1350 | |
| 1351 | static inline int is_connectable_adv(u8 evt_type) |
| 1352 | { |
| 1353 | if (evt_type == ADV_IND || evt_type == ADV_DIRECT_IND) |
| 1354 | return 1; |
| 1355 | |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1359 | int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash, |
| 1360 | u8 *randomizer) |
| 1361 | { |
| 1362 | struct oob_data *data; |
| 1363 | |
| 1364 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 1365 | |
| 1366 | if (!data) { |
| 1367 | data = kmalloc(sizeof(*data), GFP_ATOMIC); |
| 1368 | if (!data) |
| 1369 | return -ENOMEM; |
| 1370 | |
| 1371 | bacpy(&data->bdaddr, bdaddr); |
| 1372 | list_add(&data->list, &hdev->remote_oob_data); |
| 1373 | } |
| 1374 | |
| 1375 | memcpy(data->hash, hash, sizeof(data->hash)); |
| 1376 | memcpy(data->randomizer, randomizer, sizeof(data->randomizer)); |
| 1377 | |
| 1378 | BT_DBG("%s for %s", hdev->name, batostr(bdaddr)); |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Andre Guedes | 6c77c8c | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1383 | int hci_add_adv_entry(struct hci_dev *hdev, |
| 1384 | struct hci_ev_le_advertising_info *ev) |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1385 | { |
| 1386 | struct adv_entry *entry; |
Brian Gix | fdd3892 | 2011-09-28 16:23:48 -0700 | [diff] [blame] | 1387 | u8 flags = 0; |
| 1388 | int i; |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1389 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1390 | BT_DBG(""); |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1391 | |
Andre Guedes | 6c77c8c | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1392 | if (!is_connectable_adv(ev->evt_type)) |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1393 | return -EINVAL; |
| 1394 | |
Brian Gix | fdd3892 | 2011-09-28 16:23:48 -0700 | [diff] [blame] | 1395 | if (ev->data && ev->length) { |
| 1396 | for (i = 0; (i + 2) < ev->length; i++) |
| 1397 | if (ev->data[i+1] == 0x01) { |
| 1398 | flags = ev->data[i+2]; |
| 1399 | BT_DBG("flags: %2.2x", flags); |
| 1400 | break; |
| 1401 | } else { |
| 1402 | i += ev->data[i]; |
| 1403 | } |
| 1404 | } |
| 1405 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1406 | entry = hci_find_adv_entry(hdev, &ev->bdaddr); |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1407 | /* Only new entries should be added to adv_entries. So, if |
| 1408 | * bdaddr was found, don't add it. */ |
Brian Gix | fdd3892 | 2011-09-28 16:23:48 -0700 | [diff] [blame] | 1409 | if (entry) { |
| 1410 | entry->flags = flags; |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1411 | return 0; |
Brian Gix | fdd3892 | 2011-09-28 16:23:48 -0700 | [diff] [blame] | 1412 | } |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1413 | |
Andre Guedes | 6c77c8c | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1414 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1415 | if (!entry) |
| 1416 | return -ENOMEM; |
| 1417 | |
| 1418 | bacpy(&entry->bdaddr, &ev->bdaddr); |
| 1419 | entry->bdaddr_type = ev->bdaddr_type; |
Brian Gix | fdd3892 | 2011-09-28 16:23:48 -0700 | [diff] [blame] | 1420 | entry->flags = flags; |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1421 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1422 | write_lock(&hdev->adv_entries_lock); |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1423 | list_add(&entry->list, &hdev->adv_entries); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1424 | write_unlock(&hdev->adv_entries_lock); |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1425 | |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1429 | static struct crypto_blkcipher *alloc_cypher(void) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1430 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1431 | if (enable_smp) |
| 1432 | return crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1433 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1434 | return ERR_PTR(-ENOTSUPP); |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 1435 | } |
| 1436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | /* Register HCI device */ |
| 1438 | int hci_register_dev(struct hci_dev *hdev) |
| 1439 | { |
| 1440 | struct list_head *head = &hci_dev_list, *p; |
Peter Krystad | 462bf76 | 2011-09-19 14:20:20 -0700 | [diff] [blame] | 1441 | int i, id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
Marcel Holtmann | c13854ce | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 1443 | BT_DBG("%p name %s bus %d owner %p", hdev, hdev->name, |
| 1444 | hdev->bus, hdev->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
| 1446 | if (!hdev->open || !hdev->close || !hdev->destruct) |
| 1447 | return -EINVAL; |
| 1448 | |
Mat Martineau | 08add51 | 2011-11-02 16:18:36 -0700 | [diff] [blame] | 1449 | id = (hdev->dev_type == HCI_BREDR) ? 0 : 1; |
| 1450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | write_lock_bh(&hci_dev_list_lock); |
| 1452 | |
| 1453 | /* Find first available device id */ |
| 1454 | list_for_each(p, &hci_dev_list) { |
| 1455 | if (list_entry(p, struct hci_dev, list)->id != id) |
| 1456 | break; |
| 1457 | head = p; id++; |
| 1458 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | sprintf(hdev->name, "hci%d", id); |
| 1461 | hdev->id = id; |
| 1462 | list_add(&hdev->list, head); |
| 1463 | |
| 1464 | atomic_set(&hdev->refcnt, 1); |
| 1465 | spin_lock_init(&hdev->lock); |
| 1466 | |
| 1467 | hdev->flags = 0; |
| 1468 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 1469 | hdev->esco_type = (ESCO_HV1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | hdev->link_mode = (HCI_LM_ACCEPT); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1471 | hdev->io_capability = 0x03; /* No Input No Output */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1473 | hdev->idle_timeout = 0; |
| 1474 | hdev->sniff_max_interval = 800; |
| 1475 | hdev->sniff_min_interval = 80; |
| 1476 | |
Bhasker Neti | 8bfd3c5 | 2013-02-25 22:07:43 +0530 | [diff] [blame] | 1477 | set_bit(HCI_SETUP, &hdev->flags); |
| 1478 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1479 | tasklet_init(&hdev->cmd_task, hci_cmd_task, (unsigned long) hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev); |
| 1481 | tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev); |
| 1482 | |
| 1483 | skb_queue_head_init(&hdev->rx_q); |
| 1484 | skb_queue_head_init(&hdev->cmd_q); |
| 1485 | skb_queue_head_init(&hdev->raw_q); |
| 1486 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1487 | setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev); |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 1488 | setup_timer(&hdev->disco_timer, mgmt_disco_timeout, |
| 1489 | (unsigned long) hdev); |
| 1490 | setup_timer(&hdev->disco_le_timer, mgmt_disco_le_timeout, |
| 1491 | (unsigned long) hdev); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1492 | |
Suraj Sumangala | cd4c539 | 2010-07-14 13:02:16 +0530 | [diff] [blame] | 1493 | for (i = 0; i < NUM_REASSEMBLY; i++) |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1494 | hdev->reassembly[i] = NULL; |
| 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | init_waitqueue_head(&hdev->req_wait_q); |
Thomas Gleixner | a6a67ef | 2009-07-26 08:18:19 +0000 | [diff] [blame] | 1497 | mutex_init(&hdev->req_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
| 1499 | inquiry_cache_init(hdev); |
| 1500 | |
| 1501 | hci_conn_hash_init(hdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1502 | hci_chan_list_init(hdev); |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1503 | |
David Miller | ea4bd8b | 2010-07-30 21:54:49 -0700 | [diff] [blame] | 1504 | INIT_LIST_HEAD(&hdev->blacklist); |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 1505 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1506 | INIT_LIST_HEAD(&hdev->uuids); |
| 1507 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1508 | INIT_LIST_HEAD(&hdev->link_keys); |
| 1509 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1510 | INIT_LIST_HEAD(&hdev->remote_oob_data); |
| 1511 | |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1512 | INIT_LIST_HEAD(&hdev->adv_entries); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1513 | rwlock_init(&hdev->adv_entries_lock); |
| 1514 | setup_timer(&hdev->adv_timer, hci_adv_clear, (unsigned long) hdev); |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1515 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1516 | INIT_WORK(&hdev->power_on, hci_power_on); |
| 1517 | INIT_WORK(&hdev->power_off, hci_power_off); |
| 1518 | setup_timer(&hdev->off_timer, hci_auto_off, (unsigned long) hdev); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 1521 | |
| 1522 | atomic_set(&hdev->promisc, 0); |
| 1523 | |
| 1524 | write_unlock_bh(&hci_dev_list_lock); |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 1525 | |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 1526 | hdev->workqueue = create_singlethread_workqueue(hdev->name); |
| 1527 | if (!hdev->workqueue) |
| 1528 | goto nomem; |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1529 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1530 | hdev->tfm = alloc_cypher(); |
Vinicius Costa Gomes | 09fabbc | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 1531 | if (IS_ERR(hdev->tfm)) |
| 1532 | BT_INFO("Failed to load transform for ecb(aes): %ld", |
| 1533 | PTR_ERR(hdev->tfm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | |
| 1535 | hci_register_sysfs(hdev); |
| 1536 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1537 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
| 1538 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev); |
| 1539 | if (hdev->rfkill) { |
| 1540 | if (rfkill_register(hdev->rfkill) < 0) { |
| 1541 | rfkill_destroy(hdev->rfkill); |
| 1542 | hdev->rfkill = NULL; |
| 1543 | } |
| 1544 | } |
| 1545 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1546 | set_bit(HCI_AUTO_OFF, &hdev->flags); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1547 | queue_work(hdev->workqueue, &hdev->power_on); |
| 1548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | hci_notify(hdev, HCI_DEV_REG); |
| 1550 | |
| 1551 | return id; |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 1552 | |
| 1553 | nomem: |
| 1554 | write_lock_bh(&hci_dev_list_lock); |
| 1555 | list_del(&hdev->list); |
| 1556 | write_unlock_bh(&hci_dev_list_lock); |
| 1557 | |
| 1558 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | } |
| 1560 | EXPORT_SYMBOL(hci_register_dev); |
| 1561 | |
| 1562 | /* Unregister HCI device */ |
| 1563 | int hci_unregister_dev(struct hci_dev *hdev) |
| 1564 | { |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1565 | int i; |
| 1566 | |
Marcel Holtmann | c13854ce | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 1567 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | write_lock_bh(&hci_dev_list_lock); |
| 1570 | list_del(&hdev->list); |
| 1571 | write_unlock_bh(&hci_dev_list_lock); |
| 1572 | |
Ankit Verma | d3e99df | 2012-04-25 15:42:04 -0700 | [diff] [blame] | 1573 | hci_dev_do_close(hdev, hdev->bus == HCI_SMD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | |
Suraj Sumangala | cd4c539 | 2010-07-14 13:02:16 +0530 | [diff] [blame] | 1575 | for (i = 0; i < NUM_REASSEMBLY; i++) |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1576 | kfree_skb(hdev->reassembly[i]); |
| 1577 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1578 | if (!test_bit(HCI_INIT, &hdev->flags) && |
Peter Krystad | 1fc4407 | 2011-08-30 15:38:12 -0700 | [diff] [blame] | 1579 | !test_bit(HCI_SETUP, &hdev->flags) && |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1580 | hdev->dev_type == HCI_BREDR) { |
| 1581 | hci_dev_lock_bh(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1582 | mgmt_index_removed(hdev->id); |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1583 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1584 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1585 | |
Vinicius Costa Gomes | 09fabbc | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 1586 | if (!IS_ERR(hdev->tfm)) |
| 1587 | crypto_free_blkcipher(hdev->tfm); |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | hci_notify(hdev, HCI_DEV_UNREG); |
| 1590 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1591 | if (hdev->rfkill) { |
| 1592 | rfkill_unregister(hdev->rfkill); |
| 1593 | rfkill_destroy(hdev->rfkill); |
| 1594 | } |
| 1595 | |
Dave Young | 147e2d5 | 2008-03-05 18:45:59 -0800 | [diff] [blame] | 1596 | hci_unregister_sysfs(hdev); |
| 1597 | |
Brian Gix | 3cd6204 | 2012-01-11 15:18:17 -0800 | [diff] [blame] | 1598 | /* Disable all timers */ |
Gustavo F. Padovan | c6f3c5f | 2011-02-15 20:22:03 -0300 | [diff] [blame] | 1599 | hci_del_off_timer(hdev); |
Andre Guedes | 45e600f | 2011-05-26 16:23:53 -0300 | [diff] [blame] | 1600 | del_timer(&hdev->adv_timer); |
Brian Gix | 3cd6204 | 2012-01-11 15:18:17 -0800 | [diff] [blame] | 1601 | del_timer(&hdev->cmd_timer); |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 1602 | del_timer(&hdev->disco_timer); |
| 1603 | del_timer(&hdev->disco_le_timer); |
Gustavo F. Padovan | c6f3c5f | 2011-02-15 20:22:03 -0300 | [diff] [blame] | 1604 | |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 1605 | destroy_workqueue(hdev->workqueue); |
| 1606 | |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 1607 | hci_dev_lock_bh(hdev); |
| 1608 | hci_blacklist_clear(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1609 | hci_uuids_clear(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1610 | hci_link_keys_clear(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1611 | hci_remote_oob_data_clear(hdev); |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1612 | hci_adv_entries_clear(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 1613 | hci_dev_unlock_bh(hdev); |
| 1614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | __hci_dev_put(hdev); |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | return 0; |
| 1618 | } |
| 1619 | EXPORT_SYMBOL(hci_unregister_dev); |
| 1620 | |
| 1621 | /* Suspend HCI device */ |
| 1622 | int hci_suspend_dev(struct hci_dev *hdev) |
| 1623 | { |
| 1624 | hci_notify(hdev, HCI_DEV_SUSPEND); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | EXPORT_SYMBOL(hci_suspend_dev); |
| 1628 | |
| 1629 | /* Resume HCI device */ |
| 1630 | int hci_resume_dev(struct hci_dev *hdev) |
| 1631 | { |
| 1632 | hci_notify(hdev, HCI_DEV_RESUME); |
| 1633 | return 0; |
| 1634 | } |
| 1635 | EXPORT_SYMBOL(hci_resume_dev); |
| 1636 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 1637 | /* Receive frame from HCI drivers */ |
| 1638 | int hci_recv_frame(struct sk_buff *skb) |
| 1639 | { |
| 1640 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 1641 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) |
| 1642 | && !test_bit(HCI_INIT, &hdev->flags))) { |
| 1643 | kfree_skb(skb); |
| 1644 | return -ENXIO; |
| 1645 | } |
| 1646 | |
| 1647 | /* Incomming skb */ |
| 1648 | bt_cb(skb)->incoming = 1; |
| 1649 | |
| 1650 | /* Time stamp */ |
| 1651 | __net_timestamp(skb); |
| 1652 | |
| 1653 | /* Queue frame for rx task */ |
| 1654 | skb_queue_tail(&hdev->rx_q, skb); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 1655 | tasklet_schedule(&hdev->rx_task); |
| 1656 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 1657 | return 0; |
| 1658 | } |
| 1659 | EXPORT_SYMBOL(hci_recv_frame); |
| 1660 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 1661 | static int hci_reassembly(struct hci_dev *hdev, int type, void *data, |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 1662 | int count, __u8 index) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 1663 | { |
| 1664 | int len = 0; |
| 1665 | int hlen = 0; |
| 1666 | int remain = count; |
| 1667 | struct sk_buff *skb; |
| 1668 | struct bt_skb_cb *scb; |
| 1669 | |
| 1670 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || |
| 1671 | index >= NUM_REASSEMBLY) |
| 1672 | return -EILSEQ; |
| 1673 | |
| 1674 | skb = hdev->reassembly[index]; |
| 1675 | |
| 1676 | if (!skb) { |
| 1677 | switch (type) { |
| 1678 | case HCI_ACLDATA_PKT: |
| 1679 | len = HCI_MAX_FRAME_SIZE; |
| 1680 | hlen = HCI_ACL_HDR_SIZE; |
| 1681 | break; |
| 1682 | case HCI_EVENT_PKT: |
| 1683 | len = HCI_MAX_EVENT_SIZE; |
| 1684 | hlen = HCI_EVENT_HDR_SIZE; |
| 1685 | break; |
| 1686 | case HCI_SCODATA_PKT: |
| 1687 | len = HCI_MAX_SCO_SIZE; |
| 1688 | hlen = HCI_SCO_HDR_SIZE; |
| 1689 | break; |
| 1690 | } |
| 1691 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 1692 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 1693 | if (!skb) |
| 1694 | return -ENOMEM; |
| 1695 | |
| 1696 | scb = (void *) skb->cb; |
| 1697 | scb->expect = hlen; |
| 1698 | scb->pkt_type = type; |
| 1699 | |
| 1700 | skb->dev = (void *) hdev; |
| 1701 | hdev->reassembly[index] = skb; |
| 1702 | } |
| 1703 | |
| 1704 | while (count) { |
| 1705 | scb = (void *) skb->cb; |
| 1706 | len = min(scb->expect, (__u16)count); |
| 1707 | |
| 1708 | memcpy(skb_put(skb, len), data, len); |
| 1709 | |
| 1710 | count -= len; |
| 1711 | data += len; |
| 1712 | scb->expect -= len; |
| 1713 | remain = count; |
| 1714 | |
| 1715 | switch (type) { |
| 1716 | case HCI_EVENT_PKT: |
| 1717 | if (skb->len == HCI_EVENT_HDR_SIZE) { |
| 1718 | struct hci_event_hdr *h = hci_event_hdr(skb); |
| 1719 | scb->expect = h->plen; |
| 1720 | |
| 1721 | if (skb_tailroom(skb) < scb->expect) { |
| 1722 | kfree_skb(skb); |
| 1723 | hdev->reassembly[index] = NULL; |
| 1724 | return -ENOMEM; |
| 1725 | } |
| 1726 | } |
| 1727 | break; |
| 1728 | |
| 1729 | case HCI_ACLDATA_PKT: |
| 1730 | if (skb->len == HCI_ACL_HDR_SIZE) { |
| 1731 | struct hci_acl_hdr *h = hci_acl_hdr(skb); |
| 1732 | scb->expect = __le16_to_cpu(h->dlen); |
| 1733 | |
| 1734 | if (skb_tailroom(skb) < scb->expect) { |
| 1735 | kfree_skb(skb); |
| 1736 | hdev->reassembly[index] = NULL; |
| 1737 | return -ENOMEM; |
| 1738 | } |
| 1739 | } |
| 1740 | break; |
| 1741 | |
| 1742 | case HCI_SCODATA_PKT: |
| 1743 | if (skb->len == HCI_SCO_HDR_SIZE) { |
| 1744 | struct hci_sco_hdr *h = hci_sco_hdr(skb); |
| 1745 | scb->expect = h->dlen; |
| 1746 | |
| 1747 | if (skb_tailroom(skb) < scb->expect) { |
| 1748 | kfree_skb(skb); |
| 1749 | hdev->reassembly[index] = NULL; |
| 1750 | return -ENOMEM; |
| 1751 | } |
| 1752 | } |
| 1753 | break; |
| 1754 | } |
| 1755 | |
| 1756 | if (scb->expect == 0) { |
| 1757 | /* Complete frame */ |
| 1758 | |
| 1759 | bt_cb(skb)->pkt_type = type; |
| 1760 | hci_recv_frame(skb); |
| 1761 | |
| 1762 | hdev->reassembly[index] = NULL; |
| 1763 | return remain; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | return remain; |
| 1768 | } |
| 1769 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1770 | int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) |
| 1771 | { |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 1772 | int rem = 0; |
| 1773 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1774 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) |
| 1775 | return -EILSEQ; |
| 1776 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 1777 | while (count) { |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 1778 | rem = hci_reassembly(hdev, type, data, count, type - 1); |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 1779 | if (rem < 0) |
| 1780 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1781 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 1782 | data += (count - rem); |
| 1783 | count = rem; |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 1784 | }; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1785 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 1786 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1787 | } |
| 1788 | EXPORT_SYMBOL(hci_recv_fragment); |
| 1789 | |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 1790 | #define STREAM_REASSEMBLY 0 |
| 1791 | |
| 1792 | int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) |
| 1793 | { |
| 1794 | int type; |
| 1795 | int rem = 0; |
| 1796 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 1797 | while (count) { |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 1798 | struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; |
| 1799 | |
| 1800 | if (!skb) { |
| 1801 | struct { char type; } *pkt; |
| 1802 | |
| 1803 | /* Start of the frame */ |
| 1804 | pkt = data; |
| 1805 | type = pkt->type; |
| 1806 | |
| 1807 | data++; |
| 1808 | count--; |
| 1809 | } else |
| 1810 | type = bt_cb(skb)->pkt_type; |
| 1811 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 1812 | rem = hci_reassembly(hdev, type, data, count, |
| 1813 | STREAM_REASSEMBLY); |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 1814 | if (rem < 0) |
| 1815 | return rem; |
| 1816 | |
| 1817 | data += (count - rem); |
| 1818 | count = rem; |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 1819 | }; |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 1820 | |
| 1821 | return rem; |
| 1822 | } |
| 1823 | EXPORT_SYMBOL(hci_recv_stream_fragment); |
| 1824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | /* ---- Interface to upper protocols ---- */ |
| 1826 | |
| 1827 | /* Register/Unregister protocols. |
| 1828 | * hci_task_lock is used to ensure that no tasks are running. */ |
| 1829 | int hci_register_proto(struct hci_proto *hp) |
| 1830 | { |
| 1831 | int err = 0; |
| 1832 | |
| 1833 | BT_DBG("%p name %s id %d", hp, hp->name, hp->id); |
| 1834 | |
| 1835 | if (hp->id >= HCI_MAX_PROTO) |
| 1836 | return -EINVAL; |
| 1837 | |
| 1838 | write_lock_bh(&hci_task_lock); |
| 1839 | |
| 1840 | if (!hci_proto[hp->id]) |
| 1841 | hci_proto[hp->id] = hp; |
| 1842 | else |
| 1843 | err = -EEXIST; |
| 1844 | |
| 1845 | write_unlock_bh(&hci_task_lock); |
| 1846 | |
| 1847 | return err; |
| 1848 | } |
| 1849 | EXPORT_SYMBOL(hci_register_proto); |
| 1850 | |
| 1851 | int hci_unregister_proto(struct hci_proto *hp) |
| 1852 | { |
| 1853 | int err = 0; |
| 1854 | |
| 1855 | BT_DBG("%p name %s id %d", hp, hp->name, hp->id); |
| 1856 | |
| 1857 | if (hp->id >= HCI_MAX_PROTO) |
| 1858 | return -EINVAL; |
| 1859 | |
| 1860 | write_lock_bh(&hci_task_lock); |
| 1861 | |
| 1862 | if (hci_proto[hp->id]) |
| 1863 | hci_proto[hp->id] = NULL; |
| 1864 | else |
| 1865 | err = -ENOENT; |
| 1866 | |
| 1867 | write_unlock_bh(&hci_task_lock); |
| 1868 | |
| 1869 | return err; |
| 1870 | } |
| 1871 | EXPORT_SYMBOL(hci_unregister_proto); |
| 1872 | |
| 1873 | int hci_register_cb(struct hci_cb *cb) |
| 1874 | { |
| 1875 | BT_DBG("%p name %s", cb, cb->name); |
| 1876 | |
| 1877 | write_lock_bh(&hci_cb_list_lock); |
| 1878 | list_add(&cb->list, &hci_cb_list); |
| 1879 | write_unlock_bh(&hci_cb_list_lock); |
| 1880 | |
| 1881 | return 0; |
| 1882 | } |
| 1883 | EXPORT_SYMBOL(hci_register_cb); |
| 1884 | |
| 1885 | int hci_unregister_cb(struct hci_cb *cb) |
| 1886 | { |
| 1887 | BT_DBG("%p name %s", cb, cb->name); |
| 1888 | |
| 1889 | write_lock_bh(&hci_cb_list_lock); |
| 1890 | list_del(&cb->list); |
| 1891 | write_unlock_bh(&hci_cb_list_lock); |
| 1892 | |
| 1893 | return 0; |
| 1894 | } |
| 1895 | EXPORT_SYMBOL(hci_unregister_cb); |
| 1896 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1897 | int hci_register_amp(struct amp_mgr_cb *cb) |
| 1898 | { |
| 1899 | BT_DBG("%p", cb); |
| 1900 | |
| 1901 | write_lock_bh(&_mgr_cb_list_lock); |
| 1902 | list_add(&cb->list, &_mgr_cb_list); |
| 1903 | write_unlock_bh(&_mgr_cb_list_lock); |
| 1904 | |
| 1905 | return 0; |
| 1906 | } |
| 1907 | EXPORT_SYMBOL(hci_register_amp); |
| 1908 | |
| 1909 | int hci_unregister_amp(struct amp_mgr_cb *cb) |
| 1910 | { |
| 1911 | BT_DBG("%p", cb); |
| 1912 | |
| 1913 | write_lock_bh(&_mgr_cb_list_lock); |
| 1914 | list_del(&cb->list); |
| 1915 | write_unlock_bh(&_mgr_cb_list_lock); |
| 1916 | |
| 1917 | return 0; |
| 1918 | } |
| 1919 | EXPORT_SYMBOL(hci_unregister_amp); |
| 1920 | |
| 1921 | void hci_amp_cmd_complete(struct hci_dev *hdev, __u16 opcode, |
| 1922 | struct sk_buff *skb) |
| 1923 | { |
| 1924 | struct amp_mgr_cb *cb; |
| 1925 | |
| 1926 | BT_DBG("opcode 0x%x", opcode); |
| 1927 | |
| 1928 | read_lock_bh(&_mgr_cb_list_lock); |
| 1929 | list_for_each_entry(cb, &_mgr_cb_list, list) { |
| 1930 | if (cb->amp_cmd_complete_event) |
| 1931 | cb->amp_cmd_complete_event(hdev, opcode, skb); |
| 1932 | } |
| 1933 | read_unlock_bh(&_mgr_cb_list_lock); |
| 1934 | } |
| 1935 | |
| 1936 | void hci_amp_cmd_status(struct hci_dev *hdev, __u16 opcode, __u8 status) |
| 1937 | { |
| 1938 | struct amp_mgr_cb *cb; |
| 1939 | |
| 1940 | BT_DBG("opcode 0x%x, status %d", opcode, status); |
| 1941 | |
| 1942 | read_lock_bh(&_mgr_cb_list_lock); |
| 1943 | list_for_each_entry(cb, &_mgr_cb_list, list) { |
| 1944 | if (cb->amp_cmd_status_event) |
| 1945 | cb->amp_cmd_status_event(hdev, opcode, status); |
| 1946 | } |
| 1947 | read_unlock_bh(&_mgr_cb_list_lock); |
| 1948 | } |
| 1949 | |
| 1950 | void hci_amp_event_packet(struct hci_dev *hdev, __u8 ev_code, |
| 1951 | struct sk_buff *skb) |
| 1952 | { |
| 1953 | struct amp_mgr_cb *cb; |
| 1954 | |
| 1955 | BT_DBG("ev_code 0x%x", ev_code); |
| 1956 | |
| 1957 | read_lock_bh(&_mgr_cb_list_lock); |
| 1958 | list_for_each_entry(cb, &_mgr_cb_list, list) { |
| 1959 | if (cb->amp_event) |
| 1960 | cb->amp_event(hdev, ev_code, skb); |
| 1961 | } |
| 1962 | read_unlock_bh(&_mgr_cb_list_lock); |
| 1963 | } |
| 1964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | static int hci_send_frame(struct sk_buff *skb) |
| 1966 | { |
| 1967 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 1968 | |
| 1969 | if (!hdev) { |
| 1970 | kfree_skb(skb); |
| 1971 | return -ENODEV; |
| 1972 | } |
| 1973 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 1974 | BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 1976 | if (atomic_read(&hdev->promisc)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | /* Time stamp */ |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 1978 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1980 | hci_send_to_sock(hdev, skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 1984 | skb_orphan(skb); |
| 1985 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1986 | hci_notify(hdev, HCI_DEV_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | return hdev->send(skb); |
| 1988 | } |
| 1989 | |
| 1990 | /* Send HCI command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1991 | int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | { |
| 1993 | int len = HCI_COMMAND_HDR_SIZE + plen; |
| 1994 | struct hci_command_hdr *hdr; |
| 1995 | struct sk_buff *skb; |
| 1996 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1997 | BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | |
| 1999 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 2000 | if (!skb) { |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2001 | BT_ERR("%s no memory for command", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | return -ENOMEM; |
| 2003 | } |
| 2004 | |
| 2005 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2006 | hdr->opcode = cpu_to_le16(opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | hdr->plen = plen; |
| 2008 | |
| 2009 | if (plen) |
| 2010 | memcpy(skb_put(skb, plen), param, plen); |
| 2011 | |
| 2012 | BT_DBG("skb len %d", skb->len); |
| 2013 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2014 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | skb->dev = (void *) hdev; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2016 | |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 2017 | if (test_bit(HCI_INIT, &hdev->flags)) |
| 2018 | hdev->init_last_cmd = opcode; |
| 2019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | skb_queue_tail(&hdev->cmd_q, skb); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2021 | tasklet_schedule(&hdev->cmd_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | |
| 2023 | return 0; |
| 2024 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2025 | EXPORT_SYMBOL(hci_send_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
| 2027 | /* Get data from the previously sent command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2028 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | { |
| 2030 | struct hci_command_hdr *hdr; |
| 2031 | |
| 2032 | if (!hdev->sent_cmd) |
| 2033 | return NULL; |
| 2034 | |
| 2035 | hdr = (void *) hdev->sent_cmd->data; |
| 2036 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2037 | if (hdr->opcode != cpu_to_le16(opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | return NULL; |
| 2039 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2040 | BT_DBG("%s opcode 0x%x", hdev->name, opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
| 2042 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; |
| 2043 | } |
| 2044 | |
| 2045 | /* Send ACL data */ |
| 2046 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) |
| 2047 | { |
| 2048 | struct hci_acl_hdr *hdr; |
| 2049 | int len = skb->len; |
| 2050 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 2051 | skb_push(skb, HCI_ACL_HDR_SIZE); |
| 2052 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2053 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2054 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
| 2055 | hdr->dlen = cpu_to_le16(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2058 | void hci_send_acl(struct hci_conn *conn, struct hci_chan *chan, |
| 2059 | struct sk_buff *skb, __u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | { |
| 2061 | struct hci_dev *hdev = conn->hdev; |
| 2062 | struct sk_buff *list; |
| 2063 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2064 | BT_DBG("%s conn %p chan %p flags 0x%x", hdev->name, conn, chan, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
| 2066 | skb->dev = (void *) hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2067 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2068 | if (hdev->dev_type == HCI_BREDR) |
| 2069 | hci_add_acl_hdr(skb, conn->handle, flags); |
| 2070 | else |
| 2071 | hci_add_acl_hdr(skb, chan->ll_handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2073 | list = skb_shinfo(skb)->frag_list; |
| 2074 | if (!list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | /* Non fragmented */ |
| 2076 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); |
| 2077 | |
| 2078 | skb_queue_tail(&conn->data_q, skb); |
| 2079 | } else { |
| 2080 | /* Fragmented */ |
| 2081 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 2082 | |
| 2083 | skb_shinfo(skb)->frag_list = NULL; |
| 2084 | |
| 2085 | /* Queue all fragments atomically */ |
| 2086 | spin_lock_bh(&conn->data_q.lock); |
| 2087 | |
| 2088 | __skb_queue_tail(&conn->data_q, skb); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2089 | flags &= ~ACL_PB_MASK; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 2090 | flags |= ACL_CONT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | do { |
| 2092 | skb = list; list = list->next; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | skb->dev = (void *) hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2095 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 2096 | hci_add_acl_hdr(skb, conn->handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | |
| 2098 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 2099 | |
| 2100 | __skb_queue_tail(&conn->data_q, skb); |
| 2101 | } while (list); |
| 2102 | |
| 2103 | spin_unlock_bh(&conn->data_q.lock); |
| 2104 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2105 | |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2106 | tasklet_schedule(&hdev->tx_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | } |
| 2108 | EXPORT_SYMBOL(hci_send_acl); |
| 2109 | |
| 2110 | /* Send SCO data */ |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 2111 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | { |
| 2113 | struct hci_dev *hdev = conn->hdev; |
| 2114 | struct hci_sco_hdr hdr; |
| 2115 | |
| 2116 | BT_DBG("%s len %d", hdev->name, skb->len); |
| 2117 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2118 | hdr.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | hdr.dlen = skb->len; |
| 2120 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 2121 | skb_push(skb, HCI_SCO_HDR_SIZE); |
| 2122 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2123 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
| 2125 | skb->dev = (void *) hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2126 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | skb_queue_tail(&conn->data_q, skb); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2129 | tasklet_schedule(&hdev->tx_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | } |
| 2131 | EXPORT_SYMBOL(hci_send_sco); |
| 2132 | |
| 2133 | /* ---- HCI TX task (outgoing data) ---- */ |
Srinivas Krovvidi | 672dff3 | 2012-06-29 19:37:45 +0530 | [diff] [blame] | 2134 | /* HCI ACL Connection scheduler */ |
| 2135 | static inline struct hci_conn *hci_low_sent_acl(struct hci_dev *hdev, |
| 2136 | int *quote) |
| 2137 | { |
| 2138 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 2139 | struct hci_conn *conn = NULL; |
| 2140 | int num = 0, min = ~0, conn_num = 0; |
| 2141 | struct list_head *p; |
| 2142 | |
| 2143 | /* We don't have to lock device here. Connections are always |
| 2144 | * added and removed with TX task disabled. */ |
| 2145 | list_for_each(p, &h->list) { |
| 2146 | struct hci_conn *c; |
| 2147 | c = list_entry(p, struct hci_conn, list); |
| 2148 | if (c->type == ACL_LINK) |
| 2149 | conn_num++; |
| 2150 | |
| 2151 | if (skb_queue_empty(&c->data_q)) |
| 2152 | continue; |
| 2153 | |
| 2154 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) |
| 2155 | continue; |
| 2156 | |
| 2157 | num++; |
| 2158 | |
| 2159 | if (c->sent < min) { |
| 2160 | min = c->sent; |
| 2161 | conn = c; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | if (conn) { |
| 2166 | int cnt, q; |
| 2167 | cnt = hdev->acl_cnt; |
| 2168 | q = cnt / num; |
| 2169 | *quote = q ? q : 1; |
| 2170 | } else |
| 2171 | *quote = 0; |
| 2172 | |
| 2173 | if ((*quote == hdev->acl_cnt) && |
| 2174 | (conn->sent == (hdev->acl_pkts - 1)) && |
| 2175 | (conn_num > 1)) { |
| 2176 | *quote = 0; |
| 2177 | conn = NULL; |
| 2178 | } |
| 2179 | |
| 2180 | BT_DBG("conn %p quote %d", conn, *quote); |
| 2181 | return conn; |
| 2182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | |
| 2184 | /* HCI Connection scheduler */ |
| 2185 | static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote) |
| 2186 | { |
| 2187 | struct hci_conn_hash *h = &hdev->conn_hash; |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 2188 | struct hci_conn *conn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | int num = 0, min = ~0; |
| 2190 | struct list_head *p; |
| 2191 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2192 | /* We don't have to lock device here. Connections are always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | * added and removed with TX task disabled. */ |
| 2194 | list_for_each(p, &h->list) { |
| 2195 | struct hci_conn *c; |
| 2196 | c = list_entry(p, struct hci_conn, list); |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2197 | |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 2198 | if (c->type != type || skb_queue_empty(&c->data_q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | continue; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 2200 | |
| 2201 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) |
| 2202 | continue; |
| 2203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | num++; |
| 2205 | |
| 2206 | if (c->sent < min) { |
| 2207 | min = c->sent; |
| 2208 | conn = c; |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | if (conn) { |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2213 | int cnt, q; |
| 2214 | |
| 2215 | switch (conn->type) { |
| 2216 | case ACL_LINK: |
| 2217 | cnt = hdev->acl_cnt; |
| 2218 | break; |
| 2219 | case SCO_LINK: |
| 2220 | case ESCO_LINK: |
| 2221 | cnt = hdev->sco_cnt; |
| 2222 | break; |
| 2223 | case LE_LINK: |
| 2224 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 2225 | break; |
| 2226 | default: |
| 2227 | cnt = 0; |
| 2228 | BT_ERR("Unknown link type"); |
| 2229 | } |
| 2230 | |
| 2231 | q = cnt / num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | *quote = q ? q : 1; |
| 2233 | } else |
| 2234 | *quote = 0; |
| 2235 | |
| 2236 | BT_DBG("conn %p quote %d", conn, *quote); |
| 2237 | return conn; |
| 2238 | } |
| 2239 | |
Ville Tervo | bae1f5d | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2240 | static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | { |
| 2242 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 2243 | struct list_head *p; |
| 2244 | struct hci_conn *c; |
| 2245 | |
Ville Tervo | bae1f5d | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2246 | BT_ERR("%s link tx timeout", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | |
| 2248 | /* Kill stalled connections */ |
| 2249 | list_for_each(p, &h->list) { |
| 2250 | c = list_entry(p, struct hci_conn, list); |
Ville Tervo | bae1f5d | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2251 | if (c->type == type && c->sent) { |
| 2252 | BT_ERR("%s killing stalled connection %s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | hdev->name, batostr(&c->dst)); |
| 2254 | hci_acl_disconn(c, 0x13); |
| 2255 | } |
| 2256 | } |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | static inline void hci_sched_acl(struct hci_dev *hdev) |
| 2260 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | struct hci_conn *conn; |
| 2262 | struct sk_buff *skb; |
| 2263 | int quote; |
| 2264 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2265 | BT_DBG("%s", hdev->name); |
| 2266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 2268 | /* ACL tx timeout must be longer than maximum |
| 2269 | * link supervision timeout (40.9 seconds) */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2270 | if (hdev->acl_cnt <= 0 && |
| 2271 | time_after(jiffies, hdev->acl_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2272 | hci_link_tx_to(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | } |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2274 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2275 | while (hdev->acl_cnt > 0 && |
Srinivas Krovvidi | 672dff3 | 2012-06-29 19:37:45 +0530 | [diff] [blame] | 2276 | ((conn = hci_low_sent_acl(hdev, "e)) != NULL)) { |
| 2277 | |
| 2278 | while (quote > 0 && |
| 2279 | (skb = skb_dequeue(&conn->data_q))) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2280 | int count = 1; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 2283 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2284 | if (hdev->flow_ctl_mode == |
| 2285 | HCI_BLOCK_BASED_FLOW_CTL_MODE) |
| 2286 | /* Calculate count of blocks used by |
| 2287 | * this packet |
| 2288 | */ |
| 2289 | count = ((skb->len - HCI_ACL_HDR_SIZE - 1) / |
| 2290 | hdev->data_block_len) + 1; |
| 2291 | |
| 2292 | if (count > hdev->acl_cnt) |
| 2293 | return; |
| 2294 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 2295 | hci_conn_enter_active_mode(conn, bt_cb(skb)->force_active); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 2296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | hci_send_frame(skb); |
| 2298 | hdev->acl_last_tx = jiffies; |
| 2299 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2300 | hdev->acl_cnt -= count; |
| 2301 | quote -= count; |
| 2302 | |
| 2303 | conn->sent += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | } |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2305 | } |
| 2306 | } |
| 2307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | /* Schedule SCO */ |
| 2309 | static inline void hci_sched_sco(struct hci_dev *hdev) |
| 2310 | { |
| 2311 | struct hci_conn *conn; |
| 2312 | struct sk_buff *skb; |
| 2313 | int quote; |
| 2314 | |
| 2315 | BT_DBG("%s", hdev->name); |
| 2316 | |
| 2317 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { |
| 2318 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 2319 | BT_DBG("skb %p len %d", skb, skb->len); |
| 2320 | hci_send_frame(skb); |
| 2321 | |
| 2322 | conn->sent++; |
| 2323 | if (conn->sent == ~0) |
| 2324 | conn->sent = 0; |
| 2325 | } |
| 2326 | } |
| 2327 | } |
| 2328 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 2329 | static inline void hci_sched_esco(struct hci_dev *hdev) |
| 2330 | { |
| 2331 | struct hci_conn *conn; |
| 2332 | struct sk_buff *skb; |
| 2333 | int quote; |
| 2334 | |
| 2335 | BT_DBG("%s", hdev->name); |
| 2336 | |
| 2337 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, "e))) { |
| 2338 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 2339 | BT_DBG("skb %p len %d", skb, skb->len); |
| 2340 | hci_send_frame(skb); |
| 2341 | |
| 2342 | conn->sent++; |
| 2343 | if (conn->sent == ~0) |
| 2344 | conn->sent = 0; |
| 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2349 | static inline void hci_sched_le(struct hci_dev *hdev) |
| 2350 | { |
| 2351 | struct hci_conn *conn; |
| 2352 | struct sk_buff *skb; |
| 2353 | int quote, cnt; |
| 2354 | |
| 2355 | BT_DBG("%s", hdev->name); |
| 2356 | |
| 2357 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 2358 | /* LE tx timeout must be longer than maximum |
| 2359 | * link supervision timeout (40.9 seconds) */ |
Ville Tervo | bae1f5d | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2360 | if (!hdev->le_cnt && hdev->le_pkts && |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2361 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2362 | hci_link_tx_to(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; |
| 2366 | while (cnt && (conn = hci_low_sent(hdev, LE_LINK, "e))) { |
| 2367 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 2368 | BT_DBG("skb %p len %d", skb, skb->len); |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 2369 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2370 | hci_send_frame(skb); |
| 2371 | hdev->le_last_tx = jiffies; |
| 2372 | |
| 2373 | cnt--; |
| 2374 | conn->sent++; |
| 2375 | } |
| 2376 | } |
| 2377 | if (hdev->le_pkts) |
| 2378 | hdev->le_cnt = cnt; |
| 2379 | else |
| 2380 | hdev->acl_cnt = cnt; |
| 2381 | } |
| 2382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | static void hci_tx_task(unsigned long arg) |
| 2384 | { |
| 2385 | struct hci_dev *hdev = (struct hci_dev *) arg; |
| 2386 | struct sk_buff *skb; |
| 2387 | |
| 2388 | read_lock(&hci_task_lock); |
| 2389 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2390 | BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, |
| 2391 | hdev->sco_cnt, hdev->le_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | |
| 2393 | /* Schedule queues and send stuff to HCI driver */ |
| 2394 | |
| 2395 | hci_sched_acl(hdev); |
| 2396 | |
| 2397 | hci_sched_sco(hdev); |
| 2398 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 2399 | hci_sched_esco(hdev); |
| 2400 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2401 | hci_sched_le(hdev); |
| 2402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | /* Send next queued raw (unknown type) packet */ |
| 2404 | while ((skb = skb_dequeue(&hdev->raw_q))) |
| 2405 | hci_send_frame(skb); |
| 2406 | |
| 2407 | read_unlock(&hci_task_lock); |
| 2408 | } |
| 2409 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2410 | /* ----- HCI RX task (incoming data proccessing) ----- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | |
| 2412 | /* ACL data packet */ |
| 2413 | static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
| 2414 | { |
| 2415 | struct hci_acl_hdr *hdr = (void *) skb->data; |
| 2416 | struct hci_conn *conn; |
| 2417 | __u16 handle, flags; |
| 2418 | |
| 2419 | skb_pull(skb, HCI_ACL_HDR_SIZE); |
| 2420 | |
| 2421 | handle = __le16_to_cpu(hdr->handle); |
| 2422 | flags = hci_flags(handle); |
| 2423 | handle = hci_handle(handle); |
| 2424 | |
| 2425 | BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags); |
| 2426 | |
| 2427 | hdev->stat.acl_rx++; |
| 2428 | |
| 2429 | hci_dev_lock(hdev); |
| 2430 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 2431 | hci_dev_unlock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | if (conn) { |
| 2434 | register struct hci_proto *hp; |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 2435 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 2436 | hci_conn_enter_active_mode(conn, bt_cb(skb)->force_active); |
Johan Hedberg | 671267b | 2012-05-12 16:11:50 -0300 | [diff] [blame] | 2437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | /* Send to upper protocol */ |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2439 | hp = hci_proto[HCI_PROTO_L2CAP]; |
| 2440 | if (hp && hp->recv_acldata) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | hp->recv_acldata(conn, skb, flags); |
| 2442 | return; |
| 2443 | } |
| 2444 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2445 | BT_ERR("%s ACL packet for unknown connection handle %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | hdev->name, handle); |
| 2447 | } |
| 2448 | |
| 2449 | kfree_skb(skb); |
| 2450 | } |
| 2451 | |
| 2452 | /* SCO data packet */ |
| 2453 | static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
| 2454 | { |
| 2455 | struct hci_sco_hdr *hdr = (void *) skb->data; |
| 2456 | struct hci_conn *conn; |
| 2457 | __u16 handle; |
| 2458 | |
| 2459 | skb_pull(skb, HCI_SCO_HDR_SIZE); |
| 2460 | |
| 2461 | handle = __le16_to_cpu(hdr->handle); |
| 2462 | |
| 2463 | BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle); |
| 2464 | |
| 2465 | hdev->stat.sco_rx++; |
| 2466 | |
| 2467 | hci_dev_lock(hdev); |
| 2468 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 2469 | hci_dev_unlock(hdev); |
| 2470 | |
| 2471 | if (conn) { |
| 2472 | register struct hci_proto *hp; |
| 2473 | |
| 2474 | /* Send to upper protocol */ |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2475 | hp = hci_proto[HCI_PROTO_SCO]; |
| 2476 | if (hp && hp->recv_scodata) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | hp->recv_scodata(conn, skb); |
| 2478 | return; |
| 2479 | } |
| 2480 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2481 | BT_ERR("%s SCO packet for unknown connection handle %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | hdev->name, handle); |
| 2483 | } |
| 2484 | |
| 2485 | kfree_skb(skb); |
| 2486 | } |
| 2487 | |
Marcel Holtmann | 6516455 | 2005-10-28 19:20:48 +0200 | [diff] [blame] | 2488 | static void hci_rx_task(unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | { |
| 2490 | struct hci_dev *hdev = (struct hci_dev *) arg; |
| 2491 | struct sk_buff *skb; |
| 2492 | |
| 2493 | BT_DBG("%s", hdev->name); |
| 2494 | |
| 2495 | read_lock(&hci_task_lock); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 2496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | while ((skb = skb_dequeue(&hdev->rx_q))) { |
| 2498 | if (atomic_read(&hdev->promisc)) { |
| 2499 | /* Send copy to the sockets */ |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2500 | hci_send_to_sock(hdev, skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | if (test_bit(HCI_RAW, &hdev->flags)) { |
| 2504 | kfree_skb(skb); |
| 2505 | continue; |
| 2506 | } |
| 2507 | |
| 2508 | if (test_bit(HCI_INIT, &hdev->flags)) { |
| 2509 | /* Don't process data packets in this states. */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2510 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | case HCI_ACLDATA_PKT: |
| 2512 | case HCI_SCODATA_PKT: |
| 2513 | kfree_skb(skb); |
| 2514 | continue; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 2515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | /* Process frame */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2519 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | case HCI_EVENT_PKT: |
| 2521 | hci_event_packet(hdev, skb); |
| 2522 | break; |
| 2523 | |
| 2524 | case HCI_ACLDATA_PKT: |
| 2525 | BT_DBG("%s ACL data packet", hdev->name); |
| 2526 | hci_acldata_packet(hdev, skb); |
| 2527 | break; |
| 2528 | |
| 2529 | case HCI_SCODATA_PKT: |
| 2530 | BT_DBG("%s SCO data packet", hdev->name); |
| 2531 | hci_scodata_packet(hdev, skb); |
| 2532 | break; |
| 2533 | |
| 2534 | default: |
| 2535 | kfree_skb(skb); |
| 2536 | break; |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | read_unlock(&hci_task_lock); |
| 2541 | } |
| 2542 | |
| 2543 | static void hci_cmd_task(unsigned long arg) |
| 2544 | { |
| 2545 | struct hci_dev *hdev = (struct hci_dev *) arg; |
| 2546 | struct sk_buff *skb; |
| 2547 | |
| 2548 | BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt)); |
| 2549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | /* Send queued commands */ |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 2551 | if (atomic_read(&hdev->cmd_cnt)) { |
| 2552 | skb = skb_dequeue(&hdev->cmd_q); |
| 2553 | if (!skb) |
| 2554 | return; |
| 2555 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 2556 | kfree_skb(hdev->sent_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2558 | hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC); |
| 2559 | if (hdev->sent_cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | atomic_dec(&hdev->cmd_cnt); |
| 2561 | hci_send_frame(skb); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2562 | mod_timer(&hdev->cmd_timer, |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2563 | jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | } else { |
| 2565 | skb_queue_head(&hdev->cmd_q, skb); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2566 | tasklet_schedule(&hdev->cmd_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | } |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 2570 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2571 | module_param(enable_smp, bool, 0644); |
| 2572 | MODULE_PARM_DESC(enable_smp, "Enable SMP support (LE only)"); |