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