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 | |
David S. Miller | b1cc16b | 2012-01-22 14:45:14 -0500 | [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 ---- */ |
| 358 | static void inquiry_cache_flush(struct hci_dev *hdev) |
| 359 | { |
| 360 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 361 | struct inquiry_entry *next = cache->list, *e; |
| 362 | |
| 363 | BT_DBG("cache %p", cache); |
| 364 | |
| 365 | cache->list = NULL; |
| 366 | while ((e = next)) { |
| 367 | next = e->next; |
| 368 | kfree(e); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 373 | { |
| 374 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 375 | struct inquiry_entry *e; |
| 376 | |
| 377 | BT_DBG("cache %p, %s", cache, batostr(bdaddr)); |
| 378 | |
| 379 | for (e = cache->list; e; e = e->next) |
| 380 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 381 | break; |
| 382 | return e; |
| 383 | } |
| 384 | |
| 385 | void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data) |
| 386 | { |
| 387 | struct inquiry_cache *cache = &hdev->inq_cache; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 388 | struct inquiry_entry *ie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr)); |
| 391 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 392 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
| 393 | if (!ie) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | /* Entry not in the cache. Add new one. */ |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 395 | ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); |
| 396 | if (!ie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 398 | |
| 399 | ie->next = cache->list; |
| 400 | cache->list = ie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 403 | memcpy(&ie->data, data, sizeof(*data)); |
| 404 | ie->timestamp = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | cache->timestamp = jiffies; |
| 406 | } |
| 407 | |
| 408 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) |
| 409 | { |
| 410 | struct inquiry_cache *cache = &hdev->inq_cache; |
| 411 | struct inquiry_info *info = (struct inquiry_info *) buf; |
| 412 | struct inquiry_entry *e; |
| 413 | int copied = 0; |
| 414 | |
| 415 | for (e = cache->list; e && copied < num; e = e->next, copied++) { |
| 416 | struct inquiry_data *data = &e->data; |
| 417 | bacpy(&info->bdaddr, &data->bdaddr); |
| 418 | info->pscan_rep_mode = data->pscan_rep_mode; |
| 419 | info->pscan_period_mode = data->pscan_period_mode; |
| 420 | info->pscan_mode = data->pscan_mode; |
| 421 | memcpy(info->dev_class, data->dev_class, 3); |
| 422 | info->clock_offset = data->clock_offset; |
| 423 | info++; |
| 424 | } |
| 425 | |
| 426 | BT_DBG("cache %p, copied %d", cache, copied); |
| 427 | return copied; |
| 428 | } |
| 429 | |
| 430 | static void hci_inq_req(struct hci_dev *hdev, unsigned long opt) |
| 431 | { |
| 432 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; |
| 433 | struct hci_cp_inquiry cp; |
| 434 | |
| 435 | BT_DBG("%s", hdev->name); |
| 436 | |
| 437 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 438 | return; |
| 439 | |
| 440 | /* Start Inquiry */ |
| 441 | memcpy(&cp.lap, &ir->lap, 3); |
| 442 | cp.length = ir->length; |
| 443 | cp.num_rsp = ir->num_rsp; |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 444 | hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | int hci_inquiry(void __user *arg) |
| 448 | { |
| 449 | __u8 __user *ptr = arg; |
| 450 | struct hci_inquiry_req ir; |
| 451 | struct hci_dev *hdev; |
| 452 | int err = 0, do_inquiry = 0, max_rsp; |
| 453 | long timeo; |
| 454 | __u8 *buf; |
| 455 | |
| 456 | if (copy_from_user(&ir, ptr, sizeof(ir))) |
| 457 | return -EFAULT; |
| 458 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 459 | hdev = hci_dev_get(ir.dev_id); |
| 460 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return -ENODEV; |
| 462 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 463 | hci_dev_lock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 464 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 465 | inquiry_cache_empty(hdev) || |
| 466 | ir.flags & IREQ_CACHE_FLUSH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | inquiry_cache_flush(hdev); |
| 468 | do_inquiry = 1; |
| 469 | } |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 470 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 472 | timeo = ir.length * msecs_to_jiffies(2000); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 473 | |
| 474 | if (do_inquiry) { |
| 475 | err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo); |
| 476 | if (err < 0) |
| 477 | goto done; |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | /* for unlimited number of responses we will use buffer with 255 entries */ |
| 481 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; |
| 482 | |
| 483 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then |
| 484 | * copy it to the user space. |
| 485 | */ |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 486 | buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 487 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | err = -ENOMEM; |
| 489 | goto done; |
| 490 | } |
| 491 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 492 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 494 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | BT_DBG("num_rsp %d", ir.num_rsp); |
| 497 | |
| 498 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { |
| 499 | ptr += sizeof(ir); |
| 500 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * |
| 501 | ir.num_rsp)) |
| 502 | err = -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 503 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | err = -EFAULT; |
| 505 | |
| 506 | kfree(buf); |
| 507 | |
| 508 | done: |
| 509 | hci_dev_put(hdev); |
| 510 | return err; |
| 511 | } |
| 512 | |
| 513 | /* ---- HCI ioctl helpers ---- */ |
| 514 | |
| 515 | int hci_dev_open(__u16 dev) |
| 516 | { |
| 517 | struct hci_dev *hdev; |
| 518 | int ret = 0; |
| 519 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 520 | hdev = hci_dev_get(dev); |
| 521 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | return -ENODEV; |
| 523 | |
| 524 | BT_DBG("%s %p", hdev->name, hdev); |
| 525 | |
| 526 | hci_req_lock(hdev); |
| 527 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 528 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) { |
| 529 | ret = -ERFKILL; |
| 530 | goto done; |
| 531 | } |
| 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 534 | ret = -EALREADY; |
| 535 | goto done; |
| 536 | } |
| 537 | |
| 538 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 539 | set_bit(HCI_RAW, &hdev->flags); |
| 540 | |
Andrei Emeltchenko | 07e3b94 | 2011-11-11 17:02:15 +0200 | [diff] [blame] | 541 | /* Treat all non BR/EDR controllers as raw devices if |
| 542 | enable_hs is not set */ |
| 543 | if (hdev->dev_type != HCI_BREDR && !enable_hs) |
Marcel Holtmann | 943da25 | 2010-02-13 02:28:41 +0100 | [diff] [blame] | 544 | set_bit(HCI_RAW, &hdev->flags); |
| 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | if (hdev->open(hdev)) { |
| 547 | ret = -EIO; |
| 548 | goto done; |
| 549 | } |
| 550 | |
| 551 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 552 | atomic_set(&hdev->cmd_cnt, 1); |
| 553 | set_bit(HCI_INIT, &hdev->flags); |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 554 | hdev->init_last_cmd = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 556 | ret = __hci_request(hdev, hci_init_req, 0, |
| 557 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Andre Guedes | eead27d | 2011-06-30 19:20:55 -0300 | [diff] [blame] | 559 | if (lmp_host_le_capable(hdev)) |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 560 | ret = __hci_request(hdev, hci_le_init_req, 0, |
| 561 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | clear_bit(HCI_INIT, &hdev->flags); |
| 564 | } |
| 565 | |
| 566 | if (!ret) { |
| 567 | hci_dev_hold(hdev); |
| 568 | set_bit(HCI_UP, &hdev->flags); |
| 569 | hci_notify(hdev, HCI_DEV_UP); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 570 | if (!test_bit(HCI_SETUP, &hdev->flags)) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 571 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 572 | mgmt_powered(hdev, 1); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 573 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 574 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 575 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | /* Init failed, cleanup */ |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 577 | flush_work(&hdev->tx_work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 578 | flush_work(&hdev->cmd_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 579 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | skb_queue_purge(&hdev->cmd_q); |
| 582 | skb_queue_purge(&hdev->rx_q); |
| 583 | |
| 584 | if (hdev->flush) |
| 585 | hdev->flush(hdev); |
| 586 | |
| 587 | if (hdev->sent_cmd) { |
| 588 | kfree_skb(hdev->sent_cmd); |
| 589 | hdev->sent_cmd = NULL; |
| 590 | } |
| 591 | |
| 592 | hdev->close(hdev); |
| 593 | hdev->flags = 0; |
| 594 | } |
| 595 | |
| 596 | done: |
| 597 | hci_req_unlock(hdev); |
| 598 | hci_dev_put(hdev); |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | static int hci_dev_do_close(struct hci_dev *hdev) |
| 603 | { |
| 604 | BT_DBG("%s %p", hdev->name, hdev); |
| 605 | |
| 606 | hci_req_cancel(hdev, ENODEV); |
| 607 | hci_req_lock(hdev); |
| 608 | |
| 609 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 610 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | hci_req_unlock(hdev); |
| 612 | return 0; |
| 613 | } |
| 614 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 615 | /* Flush RX and TX works */ |
| 616 | flush_work(&hdev->tx_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 617 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 619 | if (hdev->discov_timeout > 0) { |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 620 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 621 | hdev->discov_timeout = 0; |
| 622 | } |
| 623 | |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 624 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags)) |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 625 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 626 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 627 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->flags)) |
| 628 | cancel_delayed_work(&hdev->service_cache); |
| 629 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 630 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | inquiry_cache_flush(hdev); |
| 632 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 633 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | hci_notify(hdev, HCI_DEV_DOWN); |
| 636 | |
| 637 | if (hdev->flush) |
| 638 | hdev->flush(hdev); |
| 639 | |
| 640 | /* Reset device */ |
| 641 | skb_queue_purge(&hdev->cmd_q); |
| 642 | atomic_set(&hdev->cmd_cnt, 1); |
Johan Hedberg | ca0d6c7 | 2012-02-03 21:29:40 +0200 | [diff] [blame] | 643 | if (!test_bit(HCI_RAW, &hdev->flags) && |
| 644 | test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | set_bit(HCI_INIT, &hdev->flags); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 646 | __hci_request(hdev, hci_reset_req, 0, |
Gustavo F. Padovan | cad44c2 | 2011-12-23 18:59:13 -0200 | [diff] [blame] | 647 | msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | clear_bit(HCI_INIT, &hdev->flags); |
| 649 | } |
| 650 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 651 | /* flush cmd work */ |
| 652 | flush_work(&hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | /* Drop queues */ |
| 655 | skb_queue_purge(&hdev->rx_q); |
| 656 | skb_queue_purge(&hdev->cmd_q); |
| 657 | skb_queue_purge(&hdev->raw_q); |
| 658 | |
| 659 | /* Drop last sent command */ |
| 660 | if (hdev->sent_cmd) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 661 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | kfree_skb(hdev->sent_cmd); |
| 663 | hdev->sent_cmd = NULL; |
| 664 | } |
| 665 | |
| 666 | /* After this point our queues are empty |
| 667 | * and no tasks are scheduled. */ |
| 668 | hdev->close(hdev); |
| 669 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 670 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 671 | mgmt_powered(hdev, 0); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 672 | hci_dev_unlock(hdev); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | /* Clear flags */ |
| 675 | hdev->flags = 0; |
| 676 | |
| 677 | hci_req_unlock(hdev); |
| 678 | |
| 679 | hci_dev_put(hdev); |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | int hci_dev_close(__u16 dev) |
| 684 | { |
| 685 | struct hci_dev *hdev; |
| 686 | int err; |
| 687 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 688 | hdev = hci_dev_get(dev); |
| 689 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | return -ENODEV; |
| 691 | err = hci_dev_do_close(hdev); |
| 692 | hci_dev_put(hdev); |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | int hci_dev_reset(__u16 dev) |
| 697 | { |
| 698 | struct hci_dev *hdev; |
| 699 | int ret = 0; |
| 700 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 701 | hdev = hci_dev_get(dev); |
| 702 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | return -ENODEV; |
| 704 | |
| 705 | hci_req_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
| 707 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 708 | goto done; |
| 709 | |
| 710 | /* Drop queues */ |
| 711 | skb_queue_purge(&hdev->rx_q); |
| 712 | skb_queue_purge(&hdev->cmd_q); |
| 713 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 714 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | inquiry_cache_flush(hdev); |
| 716 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 717 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | if (hdev->flush) |
| 720 | hdev->flush(hdev); |
| 721 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 722 | atomic_set(&hdev->cmd_cnt, 1); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 723 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | if (!test_bit(HCI_RAW, &hdev->flags)) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 726 | ret = __hci_request(hdev, hci_reset_req, 0, |
| 727 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | hci_req_unlock(hdev); |
| 731 | hci_dev_put(hdev); |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | int hci_dev_reset_stat(__u16 dev) |
| 736 | { |
| 737 | struct hci_dev *hdev; |
| 738 | int ret = 0; |
| 739 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 740 | hdev = hci_dev_get(dev); |
| 741 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | return -ENODEV; |
| 743 | |
| 744 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 745 | |
| 746 | hci_dev_put(hdev); |
| 747 | |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | int hci_dev_cmd(unsigned int cmd, void __user *arg) |
| 752 | { |
| 753 | struct hci_dev *hdev; |
| 754 | struct hci_dev_req dr; |
| 755 | int err = 0; |
| 756 | |
| 757 | if (copy_from_user(&dr, arg, sizeof(dr))) |
| 758 | return -EFAULT; |
| 759 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 760 | hdev = hci_dev_get(dr.dev_id); |
| 761 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | return -ENODEV; |
| 763 | |
| 764 | switch (cmd) { |
| 765 | case HCISETAUTH: |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 766 | err = hci_request(hdev, hci_auth_req, dr.dev_opt, |
| 767 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | break; |
| 769 | |
| 770 | case HCISETENCRYPT: |
| 771 | if (!lmp_encrypt_capable(hdev)) { |
| 772 | err = -EOPNOTSUPP; |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | if (!test_bit(HCI_AUTH, &hdev->flags)) { |
| 777 | /* Auth must be enabled first */ |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 778 | err = hci_request(hdev, hci_auth_req, dr.dev_opt, |
| 779 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | if (err) |
| 781 | break; |
| 782 | } |
| 783 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 784 | err = hci_request(hdev, hci_encrypt_req, dr.dev_opt, |
| 785 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | break; |
| 787 | |
| 788 | case HCISETSCAN: |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 789 | err = hci_request(hdev, hci_scan_req, dr.dev_opt, |
| 790 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | break; |
| 792 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 793 | case HCISETLINKPOL: |
| 794 | err = hci_request(hdev, hci_linkpol_req, dr.dev_opt, |
| 795 | msecs_to_jiffies(HCI_INIT_TIMEOUT)); |
| 796 | break; |
| 797 | |
| 798 | case HCISETLINKMODE: |
| 799 | hdev->link_mode = ((__u16) dr.dev_opt) & |
| 800 | (HCI_LM_MASTER | HCI_LM_ACCEPT); |
| 801 | break; |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | case HCISETPTYPE: |
| 804 | hdev->pkt_type = (__u16) dr.dev_opt; |
| 805 | break; |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | case HCISETACLMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 808 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 809 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | break; |
| 811 | |
| 812 | case HCISETSCOMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 813 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 814 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | break; |
| 816 | |
| 817 | default: |
| 818 | err = -EINVAL; |
| 819 | break; |
| 820 | } |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | hci_dev_put(hdev); |
| 823 | return err; |
| 824 | } |
| 825 | |
| 826 | int hci_get_dev_list(void __user *arg) |
| 827 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 828 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | struct hci_dev_list_req *dl; |
| 830 | struct hci_dev_req *dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | int n = 0, size, err; |
| 832 | __u16 dev_num; |
| 833 | |
| 834 | if (get_user(dev_num, (__u16 __user *) arg)) |
| 835 | return -EFAULT; |
| 836 | |
| 837 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) |
| 838 | return -EINVAL; |
| 839 | |
| 840 | size = sizeof(*dl) + dev_num * sizeof(*dr); |
| 841 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 842 | dl = kzalloc(size, GFP_KERNEL); |
| 843 | if (!dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | return -ENOMEM; |
| 845 | |
| 846 | dr = dl->dev_req; |
| 847 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 848 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 849 | list_for_each_entry(hdev, &hci_dev_list, list) { |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 850 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags)) |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 851 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 852 | |
| 853 | if (!test_bit(HCI_MGMT, &hdev->flags)) |
| 854 | set_bit(HCI_PAIRABLE, &hdev->flags); |
| 855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | (dr + n)->dev_id = hdev->id; |
| 857 | (dr + n)->dev_opt = hdev->flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (++n >= dev_num) |
| 860 | break; |
| 861 | } |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 862 | read_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
| 864 | dl->dev_num = n; |
| 865 | size = sizeof(*dl) + n * sizeof(*dr); |
| 866 | |
| 867 | err = copy_to_user(arg, dl, size); |
| 868 | kfree(dl); |
| 869 | |
| 870 | return err ? -EFAULT : 0; |
| 871 | } |
| 872 | |
| 873 | int hci_get_dev_info(void __user *arg) |
| 874 | { |
| 875 | struct hci_dev *hdev; |
| 876 | struct hci_dev_info di; |
| 877 | int err = 0; |
| 878 | |
| 879 | if (copy_from_user(&di, arg, sizeof(di))) |
| 880 | return -EFAULT; |
| 881 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 882 | hdev = hci_dev_get(di.dev_id); |
| 883 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | return -ENODEV; |
| 885 | |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 886 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags)) |
| 887 | cancel_delayed_work_sync(&hdev->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 888 | |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 889 | if (!test_bit(HCI_MGMT, &hdev->flags)) |
| 890 | set_bit(HCI_PAIRABLE, &hdev->flags); |
| 891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | strcpy(di.name, hdev->name); |
| 893 | di.bdaddr = hdev->bdaddr; |
Marcel Holtmann | 943da25 | 2010-02-13 02:28:41 +0100 | [diff] [blame] | 894 | di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | di.flags = hdev->flags; |
| 896 | di.pkt_type = hdev->pkt_type; |
| 897 | di.acl_mtu = hdev->acl_mtu; |
| 898 | di.acl_pkts = hdev->acl_pkts; |
| 899 | di.sco_mtu = hdev->sco_mtu; |
| 900 | di.sco_pkts = hdev->sco_pkts; |
| 901 | di.link_policy = hdev->link_policy; |
| 902 | di.link_mode = hdev->link_mode; |
| 903 | |
| 904 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); |
| 905 | memcpy(&di.features, &hdev->features, sizeof(di.features)); |
| 906 | |
| 907 | if (copy_to_user(arg, &di, sizeof(di))) |
| 908 | err = -EFAULT; |
| 909 | |
| 910 | hci_dev_put(hdev); |
| 911 | |
| 912 | return err; |
| 913 | } |
| 914 | |
| 915 | /* ---- Interface to HCI drivers ---- */ |
| 916 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 917 | static int hci_rfkill_set_block(void *data, bool blocked) |
| 918 | { |
| 919 | struct hci_dev *hdev = data; |
| 920 | |
| 921 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); |
| 922 | |
| 923 | if (!blocked) |
| 924 | return 0; |
| 925 | |
| 926 | hci_dev_do_close(hdev); |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | static const struct rfkill_ops hci_rfkill_ops = { |
| 932 | .set_block = hci_rfkill_set_block, |
| 933 | }; |
| 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | /* Alloc HCI device */ |
| 936 | struct hci_dev *hci_alloc_dev(void) |
| 937 | { |
| 938 | struct hci_dev *hdev; |
| 939 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 940 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | if (!hdev) |
| 942 | return NULL; |
| 943 | |
David Herrmann | 0ac7e70 | 2011-10-08 14:58:47 +0200 | [diff] [blame] | 944 | hci_init_sysfs(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | skb_queue_head_init(&hdev->driver_init); |
| 946 | |
| 947 | return hdev; |
| 948 | } |
| 949 | EXPORT_SYMBOL(hci_alloc_dev); |
| 950 | |
| 951 | /* Free HCI device */ |
| 952 | void hci_free_dev(struct hci_dev *hdev) |
| 953 | { |
| 954 | skb_queue_purge(&hdev->driver_init); |
| 955 | |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 956 | /* will free via device release */ |
| 957 | put_device(&hdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | EXPORT_SYMBOL(hci_free_dev); |
| 960 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 961 | static void hci_power_on(struct work_struct *work) |
| 962 | { |
| 963 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); |
| 964 | |
| 965 | BT_DBG("%s", hdev->name); |
| 966 | |
| 967 | if (hci_dev_open(hdev->id) < 0) |
| 968 | return; |
| 969 | |
| 970 | if (test_bit(HCI_AUTO_OFF, &hdev->flags)) |
Gustavo F. Padovan | 80b7ab3 | 2011-12-17 14:52:27 -0200 | [diff] [blame] | 971 | schedule_delayed_work(&hdev->power_off, |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 972 | msecs_to_jiffies(AUTO_OFF_TIMEOUT)); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 973 | |
| 974 | if (test_and_clear_bit(HCI_SETUP, &hdev->flags)) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 975 | mgmt_index_added(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | static void hci_power_off(struct work_struct *work) |
| 979 | { |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 980 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 981 | power_off.work); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 982 | |
| 983 | BT_DBG("%s", hdev->name); |
| 984 | |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 985 | clear_bit(HCI_AUTO_OFF, &hdev->flags); |
| 986 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 987 | hci_dev_close(hdev->id); |
| 988 | } |
| 989 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 990 | static void hci_discov_off(struct work_struct *work) |
| 991 | { |
| 992 | struct hci_dev *hdev; |
| 993 | u8 scan = SCAN_PAGE; |
| 994 | |
| 995 | hdev = container_of(work, struct hci_dev, discov_off.work); |
| 996 | |
| 997 | BT_DBG("%s", hdev->name); |
| 998 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 999 | hci_dev_lock(hdev); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1000 | |
| 1001 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
| 1002 | |
| 1003 | hdev->discov_timeout = 0; |
| 1004 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1005 | hci_dev_unlock(hdev); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1008 | int hci_uuids_clear(struct hci_dev *hdev) |
| 1009 | { |
| 1010 | struct list_head *p, *n; |
| 1011 | |
| 1012 | list_for_each_safe(p, n, &hdev->uuids) { |
| 1013 | struct bt_uuid *uuid; |
| 1014 | |
| 1015 | uuid = list_entry(p, struct bt_uuid, list); |
| 1016 | |
| 1017 | list_del(p); |
| 1018 | kfree(uuid); |
| 1019 | } |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1024 | int hci_link_keys_clear(struct hci_dev *hdev) |
| 1025 | { |
| 1026 | struct list_head *p, *n; |
| 1027 | |
| 1028 | list_for_each_safe(p, n, &hdev->link_keys) { |
| 1029 | struct link_key *key; |
| 1030 | |
| 1031 | key = list_entry(p, struct link_key, list); |
| 1032 | |
| 1033 | list_del(p); |
| 1034 | kfree(key); |
| 1035 | } |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1041 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1042 | struct link_key *k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1043 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1044 | list_for_each_entry(k, &hdev->link_keys, list) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1045 | if (bacmp(bdaddr, &k->bdaddr) == 0) |
| 1046 | return k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1047 | |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1051 | static int hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn, |
| 1052 | u8 key_type, u8 old_key_type) |
| 1053 | { |
| 1054 | /* Legacy key */ |
| 1055 | if (key_type < 0x03) |
| 1056 | return 1; |
| 1057 | |
| 1058 | /* Debug keys are insecure so don't store them persistently */ |
| 1059 | if (key_type == HCI_LK_DEBUG_COMBINATION) |
| 1060 | return 0; |
| 1061 | |
| 1062 | /* Changed combination key and there's no previous one */ |
| 1063 | if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) |
| 1064 | return 0; |
| 1065 | |
| 1066 | /* Security mode 3 case */ |
| 1067 | if (!conn) |
| 1068 | return 1; |
| 1069 | |
| 1070 | /* Neither local nor remote side had no-bonding as requirement */ |
| 1071 | if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) |
| 1072 | return 1; |
| 1073 | |
| 1074 | /* Local side had dedicated bonding as requirement */ |
| 1075 | if (conn->auth_type == 0x02 || conn->auth_type == 0x03) |
| 1076 | return 1; |
| 1077 | |
| 1078 | /* Remote side had dedicated bonding as requirement */ |
| 1079 | if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) |
| 1080 | return 1; |
| 1081 | |
| 1082 | /* If none of the above criteria match, then don't store the key |
| 1083 | * persistently */ |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1087 | struct link_key *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]) |
| 1088 | { |
| 1089 | struct link_key *k; |
| 1090 | |
| 1091 | list_for_each_entry(k, &hdev->link_keys, list) { |
| 1092 | struct key_master_id *id; |
| 1093 | |
| 1094 | if (k->type != HCI_LK_SMP_LTK) |
| 1095 | continue; |
| 1096 | |
| 1097 | if (k->dlen != sizeof(*id)) |
| 1098 | continue; |
| 1099 | |
| 1100 | id = (void *) &k->data; |
| 1101 | if (id->ediv == ediv && |
| 1102 | (memcmp(rand, id->rand, sizeof(id->rand)) == 0)) |
| 1103 | return k; |
| 1104 | } |
| 1105 | |
| 1106 | return NULL; |
| 1107 | } |
| 1108 | EXPORT_SYMBOL(hci_find_ltk); |
| 1109 | |
| 1110 | struct link_key *hci_find_link_key_type(struct hci_dev *hdev, |
| 1111 | bdaddr_t *bdaddr, u8 type) |
| 1112 | { |
| 1113 | struct link_key *k; |
| 1114 | |
| 1115 | list_for_each_entry(k, &hdev->link_keys, list) |
| 1116 | if (k->type == type && bacmp(bdaddr, &k->bdaddr) == 0) |
| 1117 | return k; |
| 1118 | |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | EXPORT_SYMBOL(hci_find_link_key_type); |
| 1122 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1123 | int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key, |
| 1124 | bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1125 | { |
| 1126 | struct link_key *key, *old_key; |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1127 | u8 old_key_type, persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1128 | |
| 1129 | old_key = hci_find_link_key(hdev, bdaddr); |
| 1130 | if (old_key) { |
| 1131 | old_key_type = old_key->type; |
| 1132 | key = old_key; |
| 1133 | } else { |
Johan Hedberg | 12adcf3 | 2011-04-28 11:29:00 -0700 | [diff] [blame] | 1134 | old_key_type = conn ? conn->key_type : 0xff; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1135 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
| 1136 | if (!key) |
| 1137 | return -ENOMEM; |
| 1138 | list_add(&key->list, &hdev->link_keys); |
| 1139 | } |
| 1140 | |
| 1141 | BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type); |
| 1142 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1143 | /* Some buggy controller combinations generate a changed |
| 1144 | * combination key for legacy pairing even when there's no |
| 1145 | * previous key */ |
| 1146 | if (type == HCI_LK_CHANGED_COMBINATION && |
| 1147 | (!conn || conn->remote_auth == 0xff) && |
Johan Hedberg | 655fe6e | 2011-04-28 11:29:01 -0700 | [diff] [blame] | 1148 | old_key_type == 0xff) { |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1149 | type = HCI_LK_COMBINATION; |
Johan Hedberg | 655fe6e | 2011-04-28 11:29:01 -0700 | [diff] [blame] | 1150 | if (conn) |
| 1151 | conn->key_type = type; |
| 1152 | } |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1153 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1154 | bacpy(&key->bdaddr, bdaddr); |
| 1155 | memcpy(key->val, val, 16); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1156 | key->pin_len = pin_len; |
| 1157 | |
Waldemar Rymarkiewicz | b6020ba | 2011-04-28 12:07:53 +0200 | [diff] [blame] | 1158 | if (type == HCI_LK_CHANGED_COMBINATION) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1159 | key->type = old_key_type; |
Johan Hedberg | 4748fed | 2011-04-28 11:29:02 -0700 | [diff] [blame] | 1160 | else |
| 1161 | key->type = type; |
| 1162 | |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1163 | if (!new_key) |
| 1164 | return 0; |
| 1165 | |
| 1166 | persistent = hci_persistent_key(hdev, conn, type, old_key_type); |
| 1167 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1168 | mgmt_new_link_key(hdev, key, persistent); |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1169 | |
| 1170 | if (!persistent) { |
| 1171 | list_del(&key->list); |
| 1172 | kfree(key); |
| 1173 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1178 | int hci_add_ltk(struct hci_dev *hdev, int new_key, bdaddr_t *bdaddr, |
Vinicius Costa Gomes | 726b4ff | 2011-07-08 18:31:45 -0300 | [diff] [blame] | 1179 | u8 key_size, __le16 ediv, u8 rand[8], u8 ltk[16]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1180 | { |
| 1181 | struct link_key *key, *old_key; |
| 1182 | struct key_master_id *id; |
| 1183 | u8 old_key_type; |
| 1184 | |
| 1185 | BT_DBG("%s addr %s", hdev->name, batostr(bdaddr)); |
| 1186 | |
| 1187 | old_key = hci_find_link_key_type(hdev, bdaddr, HCI_LK_SMP_LTK); |
| 1188 | if (old_key) { |
| 1189 | key = old_key; |
| 1190 | old_key_type = old_key->type; |
| 1191 | } else { |
| 1192 | key = kzalloc(sizeof(*key) + sizeof(*id), GFP_ATOMIC); |
| 1193 | if (!key) |
| 1194 | return -ENOMEM; |
| 1195 | list_add(&key->list, &hdev->link_keys); |
| 1196 | old_key_type = 0xff; |
| 1197 | } |
| 1198 | |
| 1199 | key->dlen = sizeof(*id); |
| 1200 | |
| 1201 | bacpy(&key->bdaddr, bdaddr); |
| 1202 | memcpy(key->val, ltk, sizeof(key->val)); |
| 1203 | key->type = HCI_LK_SMP_LTK; |
Vinicius Costa Gomes | 726b4ff | 2011-07-08 18:31:45 -0300 | [diff] [blame] | 1204 | key->pin_len = key_size; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1205 | |
| 1206 | id = (void *) &key->data; |
| 1207 | id->ediv = ediv; |
| 1208 | memcpy(id->rand, rand, sizeof(id->rand)); |
| 1209 | |
| 1210 | if (new_key) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1211 | mgmt_new_link_key(hdev, key, old_key_type); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1216 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1217 | { |
| 1218 | struct link_key *key; |
| 1219 | |
| 1220 | key = hci_find_link_key(hdev, bdaddr); |
| 1221 | if (!key) |
| 1222 | return -ENOENT; |
| 1223 | |
| 1224 | BT_DBG("%s removing %s", hdev->name, batostr(bdaddr)); |
| 1225 | |
| 1226 | list_del(&key->list); |
| 1227 | kfree(key); |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1232 | /* HCI command timer function */ |
| 1233 | static void hci_cmd_timer(unsigned long arg) |
| 1234 | { |
| 1235 | struct hci_dev *hdev = (void *) arg; |
| 1236 | |
| 1237 | BT_ERR("%s command tx timeout", hdev->name); |
| 1238 | atomic_set(&hdev->cmd_cnt, 1); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1239 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1242 | struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, |
| 1243 | bdaddr_t *bdaddr) |
| 1244 | { |
| 1245 | struct oob_data *data; |
| 1246 | |
| 1247 | list_for_each_entry(data, &hdev->remote_oob_data, list) |
| 1248 | if (bacmp(bdaddr, &data->bdaddr) == 0) |
| 1249 | return data; |
| 1250 | |
| 1251 | return NULL; |
| 1252 | } |
| 1253 | |
| 1254 | int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1255 | { |
| 1256 | struct oob_data *data; |
| 1257 | |
| 1258 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 1259 | if (!data) |
| 1260 | return -ENOENT; |
| 1261 | |
| 1262 | BT_DBG("%s removing %s", hdev->name, batostr(bdaddr)); |
| 1263 | |
| 1264 | list_del(&data->list); |
| 1265 | kfree(data); |
| 1266 | |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | int hci_remote_oob_data_clear(struct hci_dev *hdev) |
| 1271 | { |
| 1272 | struct oob_data *data, *n; |
| 1273 | |
| 1274 | list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { |
| 1275 | list_del(&data->list); |
| 1276 | kfree(data); |
| 1277 | } |
| 1278 | |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash, |
| 1283 | u8 *randomizer) |
| 1284 | { |
| 1285 | struct oob_data *data; |
| 1286 | |
| 1287 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 1288 | |
| 1289 | if (!data) { |
| 1290 | data = kmalloc(sizeof(*data), GFP_ATOMIC); |
| 1291 | if (!data) |
| 1292 | return -ENOMEM; |
| 1293 | |
| 1294 | bacpy(&data->bdaddr, bdaddr); |
| 1295 | list_add(&data->list, &hdev->remote_oob_data); |
| 1296 | } |
| 1297 | |
| 1298 | memcpy(data->hash, hash, sizeof(data->hash)); |
| 1299 | memcpy(data->randomizer, randomizer, sizeof(data->randomizer)); |
| 1300 | |
| 1301 | BT_DBG("%s for %s", hdev->name, batostr(bdaddr)); |
| 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1306 | struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, |
| 1307 | bdaddr_t *bdaddr) |
| 1308 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1309 | struct bdaddr_list *b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1310 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1311 | list_for_each_entry(b, &hdev->blacklist, list) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1312 | if (bacmp(bdaddr, &b->bdaddr) == 0) |
| 1313 | return b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1314 | |
| 1315 | return NULL; |
| 1316 | } |
| 1317 | |
| 1318 | int hci_blacklist_clear(struct hci_dev *hdev) |
| 1319 | { |
| 1320 | struct list_head *p, *n; |
| 1321 | |
| 1322 | list_for_each_safe(p, n, &hdev->blacklist) { |
| 1323 | struct bdaddr_list *b; |
| 1324 | |
| 1325 | b = list_entry(p, struct bdaddr_list, list); |
| 1326 | |
| 1327 | list_del(p); |
| 1328 | kfree(b); |
| 1329 | } |
| 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1335 | { |
| 1336 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1337 | |
| 1338 | if (bacmp(bdaddr, BDADDR_ANY) == 0) |
| 1339 | return -EBADF; |
| 1340 | |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1341 | if (hci_blacklist_lookup(hdev, bdaddr)) |
| 1342 | return -EEXIST; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1343 | |
| 1344 | entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1345 | if (!entry) |
| 1346 | return -ENOMEM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1347 | |
| 1348 | bacpy(&entry->bdaddr, bdaddr); |
| 1349 | |
| 1350 | list_add(&entry->list, &hdev->blacklist); |
| 1351 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1352 | return mgmt_device_blocked(hdev, bdaddr); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1356 | { |
| 1357 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1358 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 1359 | if (bacmp(bdaddr, BDADDR_ANY) == 0) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1360 | return hci_blacklist_clear(hdev); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1361 | |
| 1362 | entry = hci_blacklist_lookup(hdev, bdaddr); |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 1363 | if (!entry) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1364 | return -ENOENT; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1365 | |
| 1366 | list_del(&entry->list); |
| 1367 | kfree(entry); |
| 1368 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1369 | return mgmt_device_unblocked(hdev, bdaddr); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1370 | } |
| 1371 | |
Gustavo F. Padovan | db323f2 | 2011-06-20 16:39:29 -0300 | [diff] [blame] | 1372 | static void hci_clear_adv_cache(struct work_struct *work) |
Andre Guedes | 3581508 | 2011-05-26 16:23:53 -0300 | [diff] [blame] | 1373 | { |
Gustavo F. Padovan | db323f2 | 2011-06-20 16:39:29 -0300 | [diff] [blame] | 1374 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1375 | adv_work.work); |
Andre Guedes | 3581508 | 2011-05-26 16:23:53 -0300 | [diff] [blame] | 1376 | |
| 1377 | hci_dev_lock(hdev); |
| 1378 | |
| 1379 | hci_adv_entries_clear(hdev); |
| 1380 | |
| 1381 | hci_dev_unlock(hdev); |
| 1382 | } |
| 1383 | |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1384 | int hci_adv_entries_clear(struct hci_dev *hdev) |
| 1385 | { |
| 1386 | struct adv_entry *entry, *tmp; |
| 1387 | |
| 1388 | list_for_each_entry_safe(entry, tmp, &hdev->adv_entries, list) { |
| 1389 | list_del(&entry->list); |
| 1390 | kfree(entry); |
| 1391 | } |
| 1392 | |
| 1393 | BT_DBG("%s adv cache cleared", hdev->name); |
| 1394 | |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
| 1398 | struct adv_entry *hci_find_adv_entry(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1399 | { |
| 1400 | struct adv_entry *entry; |
| 1401 | |
| 1402 | list_for_each_entry(entry, &hdev->adv_entries, list) |
| 1403 | if (bacmp(bdaddr, &entry->bdaddr) == 0) |
| 1404 | return entry; |
| 1405 | |
| 1406 | return NULL; |
| 1407 | } |
| 1408 | |
| 1409 | static inline int is_connectable_adv(u8 evt_type) |
| 1410 | { |
| 1411 | if (evt_type == ADV_IND || evt_type == ADV_DIRECT_IND) |
| 1412 | return 1; |
| 1413 | |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | int hci_add_adv_entry(struct hci_dev *hdev, |
| 1418 | struct hci_ev_le_advertising_info *ev) |
| 1419 | { |
| 1420 | struct adv_entry *entry; |
| 1421 | |
| 1422 | if (!is_connectable_adv(ev->evt_type)) |
| 1423 | return -EINVAL; |
| 1424 | |
| 1425 | /* Only new entries should be added to adv_entries. So, if |
| 1426 | * bdaddr was found, don't add it. */ |
| 1427 | if (hci_find_adv_entry(hdev, &ev->bdaddr)) |
| 1428 | return 0; |
| 1429 | |
| 1430 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
| 1431 | if (!entry) |
| 1432 | return -ENOMEM; |
| 1433 | |
| 1434 | bacpy(&entry->bdaddr, &ev->bdaddr); |
| 1435 | entry->bdaddr_type = ev->bdaddr_type; |
| 1436 | |
| 1437 | list_add(&entry->list, &hdev->adv_entries); |
| 1438 | |
| 1439 | BT_DBG("%s adv entry added: address %s type %u", hdev->name, |
| 1440 | batostr(&entry->bdaddr), entry->bdaddr_type); |
| 1441 | |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | /* Register HCI device */ |
| 1446 | int hci_register_dev(struct hci_dev *hdev) |
| 1447 | { |
| 1448 | struct list_head *head = &hci_dev_list, *p; |
Mat Martineau | 08add51 | 2011-11-02 16:18:36 -0700 | [diff] [blame] | 1449 | int i, id, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 1451 | BT_DBG("%p name %s bus %d owner %p", hdev, hdev->name, |
| 1452 | hdev->bus, hdev->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | |
| 1454 | if (!hdev->open || !hdev->close || !hdev->destruct) |
| 1455 | return -EINVAL; |
| 1456 | |
Mat Martineau | 08add51 | 2011-11-02 16:18:36 -0700 | [diff] [blame] | 1457 | /* Do not allow HCI_AMP devices to register at index 0, |
| 1458 | * so the index can be used as the AMP controller ID. |
| 1459 | */ |
| 1460 | id = (hdev->dev_type == HCI_BREDR) ? 0 : 1; |
| 1461 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1462 | write_lock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | |
| 1464 | /* Find first available device id */ |
| 1465 | list_for_each(p, &hci_dev_list) { |
| 1466 | if (list_entry(p, struct hci_dev, list)->id != id) |
| 1467 | break; |
| 1468 | head = p; id++; |
| 1469 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | sprintf(hdev->name, "hci%d", id); |
| 1472 | hdev->id = id; |
Andrei Emeltchenko | c6feeb2 | 2011-11-16 17:30:20 +0200 | [diff] [blame] | 1473 | list_add_tail(&hdev->list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | |
| 1475 | atomic_set(&hdev->refcnt, 1); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1476 | mutex_init(&hdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
| 1478 | hdev->flags = 0; |
Andre Guedes | d23264a | 2011-11-25 20:53:38 -0300 | [diff] [blame] | 1479 | hdev->dev_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 1481 | hdev->esco_type = (ESCO_HV1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | hdev->link_mode = (HCI_LM_ACCEPT); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1483 | hdev->io_capability = 0x03; /* No Input No Output */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1485 | hdev->idle_timeout = 0; |
| 1486 | hdev->sniff_max_interval = 800; |
| 1487 | hdev->sniff_min_interval = 80; |
| 1488 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1489 | INIT_WORK(&hdev->rx_work, hci_rx_work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1490 | INIT_WORK(&hdev->cmd_work, hci_cmd_work); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1491 | INIT_WORK(&hdev->tx_work, hci_tx_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
| 1494 | skb_queue_head_init(&hdev->rx_q); |
| 1495 | skb_queue_head_init(&hdev->cmd_q); |
| 1496 | skb_queue_head_init(&hdev->raw_q); |
| 1497 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1498 | setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev); |
| 1499 | |
Suraj Sumangala | cd4c539 | 2010-07-14 13:02:16 +0530 | [diff] [blame] | 1500 | for (i = 0; i < NUM_REASSEMBLY; i++) |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1501 | hdev->reassembly[i] = NULL; |
| 1502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | init_waitqueue_head(&hdev->req_wait_q); |
Thomas Gleixner | a6a67ef | 2009-07-26 08:18:19 +0000 | [diff] [blame] | 1504 | mutex_init(&hdev->req_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | |
| 1506 | inquiry_cache_init(hdev); |
| 1507 | |
| 1508 | hci_conn_hash_init(hdev); |
| 1509 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1510 | INIT_LIST_HEAD(&hdev->mgmt_pending); |
| 1511 | |
David Miller | ea4bd8b | 2010-07-30 21:54:49 -0700 | [diff] [blame] | 1512 | INIT_LIST_HEAD(&hdev->blacklist); |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 1513 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1514 | INIT_LIST_HEAD(&hdev->uuids); |
| 1515 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1516 | INIT_LIST_HEAD(&hdev->link_keys); |
| 1517 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1518 | INIT_LIST_HEAD(&hdev->remote_oob_data); |
| 1519 | |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1520 | INIT_LIST_HEAD(&hdev->adv_entries); |
| 1521 | |
Gustavo F. Padovan | db323f2 | 2011-06-20 16:39:29 -0300 | [diff] [blame] | 1522 | INIT_DELAYED_WORK(&hdev->adv_work, hci_clear_adv_cache); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1523 | INIT_WORK(&hdev->power_on, hci_power_on); |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 1524 | INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1525 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1526 | INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); |
| 1527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 1529 | |
| 1530 | atomic_set(&hdev->promisc, 0); |
| 1531 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1532 | write_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
Gustavo F. Padovan | 32845eb | 2011-12-17 17:47:30 -0200 | [diff] [blame] | 1534 | hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND | |
| 1535 | WQ_MEM_RECLAIM, 1); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 1536 | if (!hdev->workqueue) { |
| 1537 | error = -ENOMEM; |
| 1538 | goto err; |
| 1539 | } |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 1540 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 1541 | error = hci_add_sysfs(hdev); |
| 1542 | if (error < 0) |
| 1543 | goto err_wqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1545 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
| 1546 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev); |
| 1547 | if (hdev->rfkill) { |
| 1548 | if (rfkill_register(hdev->rfkill) < 0) { |
| 1549 | rfkill_destroy(hdev->rfkill); |
| 1550 | hdev->rfkill = NULL; |
| 1551 | } |
| 1552 | } |
| 1553 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1554 | set_bit(HCI_AUTO_OFF, &hdev->flags); |
| 1555 | set_bit(HCI_SETUP, &hdev->flags); |
Gustavo F. Padovan | 7f97104 | 2011-12-18 12:40:32 -0200 | [diff] [blame] | 1556 | schedule_work(&hdev->power_on); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | hci_notify(hdev, HCI_DEV_REG); |
| 1559 | |
| 1560 | return id; |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 1561 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 1562 | err_wqueue: |
| 1563 | destroy_workqueue(hdev->workqueue); |
| 1564 | err: |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1565 | write_lock(&hci_dev_list_lock); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 1566 | list_del(&hdev->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1567 | write_unlock(&hci_dev_list_lock); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 1568 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 1569 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | } |
| 1571 | EXPORT_SYMBOL(hci_register_dev); |
| 1572 | |
| 1573 | /* Unregister HCI device */ |
David Herrmann | 5973563 | 2011-10-26 10:43:19 +0200 | [diff] [blame] | 1574 | void hci_unregister_dev(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | { |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1576 | int i; |
| 1577 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 1578 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1580 | write_lock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | list_del(&hdev->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1582 | write_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | |
| 1584 | hci_dev_do_close(hdev); |
| 1585 | |
Suraj Sumangala | cd4c539 | 2010-07-14 13:02:16 +0530 | [diff] [blame] | 1586 | for (i = 0; i < NUM_REASSEMBLY; i++) |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1587 | kfree_skb(hdev->reassembly[i]); |
| 1588 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1589 | if (!test_bit(HCI_INIT, &hdev->flags) && |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1590 | !test_bit(HCI_SETUP, &hdev->flags)) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1591 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1592 | mgmt_index_removed(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1593 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1594 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1595 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1596 | /* mgmt_index_removed should take care of emptying the |
| 1597 | * pending list */ |
| 1598 | BUG_ON(!list_empty(&hdev->mgmt_pending)); |
| 1599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | hci_notify(hdev, HCI_DEV_UNREG); |
| 1601 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1602 | if (hdev->rfkill) { |
| 1603 | rfkill_unregister(hdev->rfkill); |
| 1604 | rfkill_destroy(hdev->rfkill); |
| 1605 | } |
| 1606 | |
David Herrmann | ce24297 | 2011-10-08 14:58:48 +0200 | [diff] [blame] | 1607 | hci_del_sysfs(hdev); |
Dave Young | 147e2d5 | 2008-03-05 18:45:59 -0800 | [diff] [blame] | 1608 | |
Gustavo F. Padovan | db323f2 | 2011-06-20 16:39:29 -0300 | [diff] [blame] | 1609 | cancel_delayed_work_sync(&hdev->adv_work); |
Gustavo F. Padovan | c6f3c5f | 2011-02-15 20:22:03 -0300 | [diff] [blame] | 1610 | |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 1611 | destroy_workqueue(hdev->workqueue); |
| 1612 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1613 | hci_dev_lock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 1614 | hci_blacklist_clear(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1615 | hci_uuids_clear(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1616 | hci_link_keys_clear(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1617 | hci_remote_oob_data_clear(hdev); |
Andre Guedes | 76c8686 | 2011-05-26 16:23:50 -0300 | [diff] [blame] | 1618 | hci_adv_entries_clear(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1619 | hci_dev_unlock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 1620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | __hci_dev_put(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | } |
| 1623 | EXPORT_SYMBOL(hci_unregister_dev); |
| 1624 | |
| 1625 | /* Suspend HCI device */ |
| 1626 | int hci_suspend_dev(struct hci_dev *hdev) |
| 1627 | { |
| 1628 | hci_notify(hdev, HCI_DEV_SUSPEND); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | EXPORT_SYMBOL(hci_suspend_dev); |
| 1632 | |
| 1633 | /* Resume HCI device */ |
| 1634 | int hci_resume_dev(struct hci_dev *hdev) |
| 1635 | { |
| 1636 | hci_notify(hdev, HCI_DEV_RESUME); |
| 1637 | return 0; |
| 1638 | } |
| 1639 | EXPORT_SYMBOL(hci_resume_dev); |
| 1640 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 1641 | /* Receive frame from HCI drivers */ |
| 1642 | int hci_recv_frame(struct sk_buff *skb) |
| 1643 | { |
| 1644 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 1645 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) |
| 1646 | && !test_bit(HCI_INIT, &hdev->flags))) { |
| 1647 | kfree_skb(skb); |
| 1648 | return -ENXIO; |
| 1649 | } |
| 1650 | |
| 1651 | /* Incomming skb */ |
| 1652 | bt_cb(skb)->incoming = 1; |
| 1653 | |
| 1654 | /* Time stamp */ |
| 1655 | __net_timestamp(skb); |
| 1656 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 1657 | skb_queue_tail(&hdev->rx_q, skb); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1658 | queue_work(hdev->workqueue, &hdev->rx_work); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 1659 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 1660 | return 0; |
| 1661 | } |
| 1662 | EXPORT_SYMBOL(hci_recv_frame); |
| 1663 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 1664 | 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] | 1665 | int count, __u8 index) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 1666 | { |
| 1667 | int len = 0; |
| 1668 | int hlen = 0; |
| 1669 | int remain = count; |
| 1670 | struct sk_buff *skb; |
| 1671 | struct bt_skb_cb *scb; |
| 1672 | |
| 1673 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || |
| 1674 | index >= NUM_REASSEMBLY) |
| 1675 | return -EILSEQ; |
| 1676 | |
| 1677 | skb = hdev->reassembly[index]; |
| 1678 | |
| 1679 | if (!skb) { |
| 1680 | switch (type) { |
| 1681 | case HCI_ACLDATA_PKT: |
| 1682 | len = HCI_MAX_FRAME_SIZE; |
| 1683 | hlen = HCI_ACL_HDR_SIZE; |
| 1684 | break; |
| 1685 | case HCI_EVENT_PKT: |
| 1686 | len = HCI_MAX_EVENT_SIZE; |
| 1687 | hlen = HCI_EVENT_HDR_SIZE; |
| 1688 | break; |
| 1689 | case HCI_SCODATA_PKT: |
| 1690 | len = HCI_MAX_SCO_SIZE; |
| 1691 | hlen = HCI_SCO_HDR_SIZE; |
| 1692 | break; |
| 1693 | } |
| 1694 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 1695 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 1696 | if (!skb) |
| 1697 | return -ENOMEM; |
| 1698 | |
| 1699 | scb = (void *) skb->cb; |
| 1700 | scb->expect = hlen; |
| 1701 | scb->pkt_type = type; |
| 1702 | |
| 1703 | skb->dev = (void *) hdev; |
| 1704 | hdev->reassembly[index] = skb; |
| 1705 | } |
| 1706 | |
| 1707 | while (count) { |
| 1708 | scb = (void *) skb->cb; |
| 1709 | len = min(scb->expect, (__u16)count); |
| 1710 | |
| 1711 | memcpy(skb_put(skb, len), data, len); |
| 1712 | |
| 1713 | count -= len; |
| 1714 | data += len; |
| 1715 | scb->expect -= len; |
| 1716 | remain = count; |
| 1717 | |
| 1718 | switch (type) { |
| 1719 | case HCI_EVENT_PKT: |
| 1720 | if (skb->len == HCI_EVENT_HDR_SIZE) { |
| 1721 | struct hci_event_hdr *h = hci_event_hdr(skb); |
| 1722 | scb->expect = h->plen; |
| 1723 | |
| 1724 | if (skb_tailroom(skb) < scb->expect) { |
| 1725 | kfree_skb(skb); |
| 1726 | hdev->reassembly[index] = NULL; |
| 1727 | return -ENOMEM; |
| 1728 | } |
| 1729 | } |
| 1730 | break; |
| 1731 | |
| 1732 | case HCI_ACLDATA_PKT: |
| 1733 | if (skb->len == HCI_ACL_HDR_SIZE) { |
| 1734 | struct hci_acl_hdr *h = hci_acl_hdr(skb); |
| 1735 | scb->expect = __le16_to_cpu(h->dlen); |
| 1736 | |
| 1737 | if (skb_tailroom(skb) < scb->expect) { |
| 1738 | kfree_skb(skb); |
| 1739 | hdev->reassembly[index] = NULL; |
| 1740 | return -ENOMEM; |
| 1741 | } |
| 1742 | } |
| 1743 | break; |
| 1744 | |
| 1745 | case HCI_SCODATA_PKT: |
| 1746 | if (skb->len == HCI_SCO_HDR_SIZE) { |
| 1747 | struct hci_sco_hdr *h = hci_sco_hdr(skb); |
| 1748 | scb->expect = h->dlen; |
| 1749 | |
| 1750 | if (skb_tailroom(skb) < scb->expect) { |
| 1751 | kfree_skb(skb); |
| 1752 | hdev->reassembly[index] = NULL; |
| 1753 | return -ENOMEM; |
| 1754 | } |
| 1755 | } |
| 1756 | break; |
| 1757 | } |
| 1758 | |
| 1759 | if (scb->expect == 0) { |
| 1760 | /* Complete frame */ |
| 1761 | |
| 1762 | bt_cb(skb)->pkt_type = type; |
| 1763 | hci_recv_frame(skb); |
| 1764 | |
| 1765 | hdev->reassembly[index] = NULL; |
| 1766 | return remain; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | return remain; |
| 1771 | } |
| 1772 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1773 | int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) |
| 1774 | { |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 1775 | int rem = 0; |
| 1776 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1777 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) |
| 1778 | return -EILSEQ; |
| 1779 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 1780 | while (count) { |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 1781 | rem = hci_reassembly(hdev, type, data, count, type - 1); |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 1782 | if (rem < 0) |
| 1783 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1784 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 1785 | data += (count - rem); |
| 1786 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 1787 | } |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1788 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 1789 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1790 | } |
| 1791 | EXPORT_SYMBOL(hci_recv_fragment); |
| 1792 | |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 1793 | #define STREAM_REASSEMBLY 0 |
| 1794 | |
| 1795 | int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) |
| 1796 | { |
| 1797 | int type; |
| 1798 | int rem = 0; |
| 1799 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 1800 | while (count) { |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 1801 | struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; |
| 1802 | |
| 1803 | if (!skb) { |
| 1804 | struct { char type; } *pkt; |
| 1805 | |
| 1806 | /* Start of the frame */ |
| 1807 | pkt = data; |
| 1808 | type = pkt->type; |
| 1809 | |
| 1810 | data++; |
| 1811 | count--; |
| 1812 | } else |
| 1813 | type = bt_cb(skb)->pkt_type; |
| 1814 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 1815 | rem = hci_reassembly(hdev, type, data, count, |
| 1816 | STREAM_REASSEMBLY); |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 1817 | if (rem < 0) |
| 1818 | return rem; |
| 1819 | |
| 1820 | data += (count - rem); |
| 1821 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 1822 | } |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 1823 | |
| 1824 | return rem; |
| 1825 | } |
| 1826 | EXPORT_SYMBOL(hci_recv_stream_fragment); |
| 1827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | /* ---- Interface to upper protocols ---- */ |
| 1829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | int hci_register_cb(struct hci_cb *cb) |
| 1831 | { |
| 1832 | BT_DBG("%p name %s", cb, cb->name); |
| 1833 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1834 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | list_add(&cb->list, &hci_cb_list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1836 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | |
| 1838 | return 0; |
| 1839 | } |
| 1840 | EXPORT_SYMBOL(hci_register_cb); |
| 1841 | |
| 1842 | int hci_unregister_cb(struct hci_cb *cb) |
| 1843 | { |
| 1844 | BT_DBG("%p name %s", cb, cb->name); |
| 1845 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1846 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | list_del(&cb->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1848 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | |
| 1850 | return 0; |
| 1851 | } |
| 1852 | EXPORT_SYMBOL(hci_unregister_cb); |
| 1853 | |
| 1854 | static int hci_send_frame(struct sk_buff *skb) |
| 1855 | { |
| 1856 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 1857 | |
| 1858 | if (!hdev) { |
| 1859 | kfree_skb(skb); |
| 1860 | return -ENODEV; |
| 1861 | } |
| 1862 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 1863 | 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] | 1864 | |
| 1865 | if (atomic_read(&hdev->promisc)) { |
| 1866 | /* Time stamp */ |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 1867 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1869 | hci_send_to_sock(hdev, skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 1873 | skb_orphan(skb); |
| 1874 | |
| 1875 | return hdev->send(skb); |
| 1876 | } |
| 1877 | |
| 1878 | /* Send HCI command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1879 | 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] | 1880 | { |
| 1881 | int len = HCI_COMMAND_HDR_SIZE + plen; |
| 1882 | struct hci_command_hdr *hdr; |
| 1883 | struct sk_buff *skb; |
| 1884 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1885 | BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | |
| 1887 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 1888 | if (!skb) { |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 1889 | BT_ERR("%s no memory for command", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | return -ENOMEM; |
| 1891 | } |
| 1892 | |
| 1893 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1894 | hdr->opcode = cpu_to_le16(opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | hdr->plen = plen; |
| 1896 | |
| 1897 | if (plen) |
| 1898 | memcpy(skb_put(skb, plen), param, plen); |
| 1899 | |
| 1900 | BT_DBG("skb len %d", skb->len); |
| 1901 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 1902 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | skb->dev = (void *) hdev; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 1904 | |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 1905 | if (test_bit(HCI_INIT, &hdev->flags)) |
| 1906 | hdev->init_last_cmd = opcode; |
| 1907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1909 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | |
| 1911 | return 0; |
| 1912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | |
| 1914 | /* Get data from the previously sent command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1915 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | { |
| 1917 | struct hci_command_hdr *hdr; |
| 1918 | |
| 1919 | if (!hdev->sent_cmd) |
| 1920 | return NULL; |
| 1921 | |
| 1922 | hdr = (void *) hdev->sent_cmd->data; |
| 1923 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1924 | if (hdr->opcode != cpu_to_le16(opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | return NULL; |
| 1926 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1927 | BT_DBG("%s opcode 0x%x", hdev->name, opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | |
| 1929 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; |
| 1930 | } |
| 1931 | |
| 1932 | /* Send ACL data */ |
| 1933 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) |
| 1934 | { |
| 1935 | struct hci_acl_hdr *hdr; |
| 1936 | int len = skb->len; |
| 1937 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 1938 | skb_push(skb, HCI_ACL_HDR_SIZE); |
| 1939 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1940 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1941 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
| 1942 | hdr->dlen = cpu_to_le16(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1945 | static void hci_queue_acl(struct hci_conn *conn, struct sk_buff_head *queue, |
| 1946 | struct sk_buff *skb, __u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | { |
| 1948 | struct hci_dev *hdev = conn->hdev; |
| 1949 | struct sk_buff *list; |
| 1950 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1951 | list = skb_shinfo(skb)->frag_list; |
| 1952 | if (!list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | /* Non fragmented */ |
| 1954 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); |
| 1955 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1956 | skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | } else { |
| 1958 | /* Fragmented */ |
| 1959 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 1960 | |
| 1961 | skb_shinfo(skb)->frag_list = NULL; |
| 1962 | |
| 1963 | /* Queue all fragments atomically */ |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 1964 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1966 | __skb_queue_tail(queue, skb); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 1967 | |
| 1968 | flags &= ~ACL_START; |
| 1969 | flags |= ACL_CONT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | do { |
| 1971 | skb = list; list = list->next; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | skb->dev = (void *) hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 1974 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 1975 | hci_add_acl_hdr(skb, conn->handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | |
| 1977 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 1978 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1979 | __skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | } while (list); |
| 1981 | |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 1982 | spin_unlock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) |
| 1987 | { |
| 1988 | struct hci_conn *conn = chan->conn; |
| 1989 | struct hci_dev *hdev = conn->hdev; |
| 1990 | |
| 1991 | BT_DBG("%s chan %p flags 0x%x", hdev->name, chan, flags); |
| 1992 | |
| 1993 | skb->dev = (void *) hdev; |
| 1994 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
| 1995 | hci_add_acl_hdr(skb, conn->handle, flags); |
| 1996 | |
| 1997 | hci_queue_acl(conn, &chan->data_q, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1999 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | } |
| 2001 | EXPORT_SYMBOL(hci_send_acl); |
| 2002 | |
| 2003 | /* Send SCO data */ |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 2004 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | { |
| 2006 | struct hci_dev *hdev = conn->hdev; |
| 2007 | struct hci_sco_hdr hdr; |
| 2008 | |
| 2009 | BT_DBG("%s len %d", hdev->name, skb->len); |
| 2010 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2011 | hdr.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | hdr.dlen = skb->len; |
| 2013 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 2014 | skb_push(skb, HCI_SCO_HDR_SIZE); |
| 2015 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2016 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | |
| 2018 | skb->dev = (void *) hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2019 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | skb_queue_tail(&conn->data_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2022 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | } |
| 2024 | EXPORT_SYMBOL(hci_send_sco); |
| 2025 | |
| 2026 | /* ---- HCI TX task (outgoing data) ---- */ |
| 2027 | |
| 2028 | /* HCI Connection scheduler */ |
| 2029 | static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote) |
| 2030 | { |
| 2031 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2032 | struct hci_conn *conn = NULL, *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | int num = 0, min = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2035 | /* We don't have to lock device here. Connections are always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | * added and removed with TX task disabled. */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2037 | |
| 2038 | rcu_read_lock(); |
| 2039 | |
| 2040 | list_for_each_entry_rcu(c, &h->list, list) { |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 2041 | if (c->type != type || skb_queue_empty(&c->data_q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | continue; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 2043 | |
| 2044 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) |
| 2045 | continue; |
| 2046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | num++; |
| 2048 | |
| 2049 | if (c->sent < min) { |
| 2050 | min = c->sent; |
| 2051 | conn = c; |
| 2052 | } |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 2053 | |
| 2054 | if (hci_conn_num(hdev, type) == num) |
| 2055 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2058 | rcu_read_unlock(); |
| 2059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | if (conn) { |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2061 | int cnt, q; |
| 2062 | |
| 2063 | switch (conn->type) { |
| 2064 | case ACL_LINK: |
| 2065 | cnt = hdev->acl_cnt; |
| 2066 | break; |
| 2067 | case SCO_LINK: |
| 2068 | case ESCO_LINK: |
| 2069 | cnt = hdev->sco_cnt; |
| 2070 | break; |
| 2071 | case LE_LINK: |
| 2072 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 2073 | break; |
| 2074 | default: |
| 2075 | cnt = 0; |
| 2076 | BT_ERR("Unknown link type"); |
| 2077 | } |
| 2078 | |
| 2079 | q = cnt / num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | *quote = q ? q : 1; |
| 2081 | } else |
| 2082 | *quote = 0; |
| 2083 | |
| 2084 | BT_DBG("conn %p quote %d", conn, *quote); |
| 2085 | return conn; |
| 2086 | } |
| 2087 | |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2088 | 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] | 2089 | { |
| 2090 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2091 | struct hci_conn *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2093 | BT_ERR("%s link tx timeout", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2095 | rcu_read_lock(); |
| 2096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | /* Kill stalled connections */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2098 | list_for_each_entry_rcu(c, &h->list, list) { |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2099 | if (c->type == type && c->sent) { |
| 2100 | BT_ERR("%s killing stalled connection %s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | hdev->name, batostr(&c->dst)); |
| 2102 | hci_acl_disconn(c, 0x13); |
| 2103 | } |
| 2104 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2105 | |
| 2106 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2109 | static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, |
| 2110 | int *quote) |
| 2111 | { |
| 2112 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 2113 | struct hci_chan *chan = NULL; |
| 2114 | int num = 0, min = ~0, cur_prio = 0; |
| 2115 | struct hci_conn *conn; |
| 2116 | int cnt, q, conn_num = 0; |
| 2117 | |
| 2118 | BT_DBG("%s", hdev->name); |
| 2119 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2120 | rcu_read_lock(); |
| 2121 | |
| 2122 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2123 | struct hci_chan *tmp; |
| 2124 | |
| 2125 | if (conn->type != type) |
| 2126 | continue; |
| 2127 | |
| 2128 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 2129 | continue; |
| 2130 | |
| 2131 | conn_num++; |
| 2132 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 2133 | list_for_each_entry_rcu(tmp, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2134 | struct sk_buff *skb; |
| 2135 | |
| 2136 | if (skb_queue_empty(&tmp->data_q)) |
| 2137 | continue; |
| 2138 | |
| 2139 | skb = skb_peek(&tmp->data_q); |
| 2140 | if (skb->priority < cur_prio) |
| 2141 | continue; |
| 2142 | |
| 2143 | if (skb->priority > cur_prio) { |
| 2144 | num = 0; |
| 2145 | min = ~0; |
| 2146 | cur_prio = skb->priority; |
| 2147 | } |
| 2148 | |
| 2149 | num++; |
| 2150 | |
| 2151 | if (conn->sent < min) { |
| 2152 | min = conn->sent; |
| 2153 | chan = tmp; |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | if (hci_conn_num(hdev, type) == conn_num) |
| 2158 | break; |
| 2159 | } |
| 2160 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2161 | rcu_read_unlock(); |
| 2162 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2163 | if (!chan) |
| 2164 | return NULL; |
| 2165 | |
| 2166 | switch (chan->conn->type) { |
| 2167 | case ACL_LINK: |
| 2168 | cnt = hdev->acl_cnt; |
| 2169 | break; |
| 2170 | case SCO_LINK: |
| 2171 | case ESCO_LINK: |
| 2172 | cnt = hdev->sco_cnt; |
| 2173 | break; |
| 2174 | case LE_LINK: |
| 2175 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 2176 | break; |
| 2177 | default: |
| 2178 | cnt = 0; |
| 2179 | BT_ERR("Unknown link type"); |
| 2180 | } |
| 2181 | |
| 2182 | q = cnt / num; |
| 2183 | *quote = q ? q : 1; |
| 2184 | BT_DBG("chan %p quote %d", chan, *quote); |
| 2185 | return chan; |
| 2186 | } |
| 2187 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2188 | static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) |
| 2189 | { |
| 2190 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 2191 | struct hci_conn *conn; |
| 2192 | int num = 0; |
| 2193 | |
| 2194 | BT_DBG("%s", hdev->name); |
| 2195 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2196 | rcu_read_lock(); |
| 2197 | |
| 2198 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2199 | struct hci_chan *chan; |
| 2200 | |
| 2201 | if (conn->type != type) |
| 2202 | continue; |
| 2203 | |
| 2204 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 2205 | continue; |
| 2206 | |
| 2207 | num++; |
| 2208 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 2209 | list_for_each_entry_rcu(chan, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2210 | struct sk_buff *skb; |
| 2211 | |
| 2212 | if (chan->sent) { |
| 2213 | chan->sent = 0; |
| 2214 | continue; |
| 2215 | } |
| 2216 | |
| 2217 | if (skb_queue_empty(&chan->data_q)) |
| 2218 | continue; |
| 2219 | |
| 2220 | skb = skb_peek(&chan->data_q); |
| 2221 | if (skb->priority >= HCI_PRIO_MAX - 1) |
| 2222 | continue; |
| 2223 | |
| 2224 | skb->priority = HCI_PRIO_MAX - 1; |
| 2225 | |
| 2226 | BT_DBG("chan %p skb %p promoted to %d", chan, skb, |
| 2227 | skb->priority); |
| 2228 | } |
| 2229 | |
| 2230 | if (hci_conn_num(hdev, type) == num) |
| 2231 | break; |
| 2232 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2233 | |
| 2234 | rcu_read_unlock(); |
| 2235 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | static inline void hci_sched_acl(struct hci_dev *hdev) |
| 2239 | { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2240 | struct hci_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | struct sk_buff *skb; |
| 2242 | int quote; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2243 | unsigned int cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
| 2245 | BT_DBG("%s", hdev->name); |
| 2246 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 2247 | if (!hci_conn_num(hdev, ACL_LINK)) |
| 2248 | return; |
| 2249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 2251 | /* ACL tx timeout must be longer than maximum |
| 2252 | * link supervision timeout (40.9 seconds) */ |
S.Çağlar Onur | 82453021 | 2008-02-17 23:25:57 -0800 | [diff] [blame] | 2253 | if (!hdev->acl_cnt && time_after(jiffies, hdev->acl_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2254 | hci_link_tx_to(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2257 | cnt = hdev->acl_cnt; |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 2258 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2259 | while (hdev->acl_cnt && |
| 2260 | (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 2261 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 2262 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2263 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
| 2264 | skb->len, skb->priority); |
| 2265 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 2266 | /* Stop if priority has changed */ |
| 2267 | if (skb->priority < priority) |
| 2268 | break; |
| 2269 | |
| 2270 | skb = skb_dequeue(&chan->data_q); |
| 2271 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2272 | hci_conn_enter_active_mode(chan->conn, |
| 2273 | bt_cb(skb)->force_active); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 2274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | hci_send_frame(skb); |
| 2276 | hdev->acl_last_tx = jiffies; |
| 2277 | |
| 2278 | hdev->acl_cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2279 | chan->sent++; |
| 2280 | chan->conn->sent++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | } |
| 2282 | } |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2283 | |
| 2284 | if (cnt != hdev->acl_cnt) |
| 2285 | hci_prio_recalculate(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | /* Schedule SCO */ |
| 2289 | static inline void hci_sched_sco(struct hci_dev *hdev) |
| 2290 | { |
| 2291 | struct hci_conn *conn; |
| 2292 | struct sk_buff *skb; |
| 2293 | int quote; |
| 2294 | |
| 2295 | BT_DBG("%s", hdev->name); |
| 2296 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 2297 | if (!hci_conn_num(hdev, SCO_LINK)) |
| 2298 | return; |
| 2299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { |
| 2301 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 2302 | BT_DBG("skb %p len %d", skb, skb->len); |
| 2303 | hci_send_frame(skb); |
| 2304 | |
| 2305 | conn->sent++; |
| 2306 | if (conn->sent == ~0) |
| 2307 | conn->sent = 0; |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 2312 | static inline void hci_sched_esco(struct hci_dev *hdev) |
| 2313 | { |
| 2314 | struct hci_conn *conn; |
| 2315 | struct sk_buff *skb; |
| 2316 | int quote; |
| 2317 | |
| 2318 | BT_DBG("%s", hdev->name); |
| 2319 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 2320 | if (!hci_conn_num(hdev, ESCO_LINK)) |
| 2321 | return; |
| 2322 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 2323 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, "e))) { |
| 2324 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 2325 | BT_DBG("skb %p len %d", skb, skb->len); |
| 2326 | hci_send_frame(skb); |
| 2327 | |
| 2328 | conn->sent++; |
| 2329 | if (conn->sent == ~0) |
| 2330 | conn->sent = 0; |
| 2331 | } |
| 2332 | } |
| 2333 | } |
| 2334 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2335 | static inline void hci_sched_le(struct hci_dev *hdev) |
| 2336 | { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2337 | struct hci_chan *chan; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2338 | struct sk_buff *skb; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2339 | int quote, cnt, tmp; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2340 | |
| 2341 | BT_DBG("%s", hdev->name); |
| 2342 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 2343 | if (!hci_conn_num(hdev, LE_LINK)) |
| 2344 | return; |
| 2345 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2346 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 2347 | /* LE tx timeout must be longer than maximum |
| 2348 | * link supervision timeout (40.9 seconds) */ |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2349 | if (!hdev->le_cnt && hdev->le_pkts && |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2350 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2351 | hci_link_tx_to(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2355 | tmp = cnt; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2356 | while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 2357 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 2358 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2359 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
| 2360 | skb->len, skb->priority); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2361 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 2362 | /* Stop if priority has changed */ |
| 2363 | if (skb->priority < priority) |
| 2364 | break; |
| 2365 | |
| 2366 | skb = skb_dequeue(&chan->data_q); |
| 2367 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2368 | hci_send_frame(skb); |
| 2369 | hdev->le_last_tx = jiffies; |
| 2370 | |
| 2371 | cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2372 | chan->sent++; |
| 2373 | chan->conn->sent++; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2374 | } |
| 2375 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2376 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2377 | if (hdev->le_pkts) |
| 2378 | hdev->le_cnt = cnt; |
| 2379 | else |
| 2380 | hdev->acl_cnt = cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2381 | |
| 2382 | if (cnt != tmp) |
| 2383 | hci_prio_recalculate(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2384 | } |
| 2385 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2386 | static void hci_tx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | { |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2388 | struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | struct sk_buff *skb; |
| 2390 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2391 | BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, |
| 2392 | hdev->sco_cnt, hdev->le_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | |
| 2394 | /* Schedule queues and send stuff to HCI driver */ |
| 2395 | |
| 2396 | hci_sched_acl(hdev); |
| 2397 | |
| 2398 | hci_sched_sco(hdev); |
| 2399 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 2400 | hci_sched_esco(hdev); |
| 2401 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2402 | hci_sched_le(hdev); |
| 2403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | /* Send next queued raw (unknown type) packet */ |
| 2405 | while ((skb = skb_dequeue(&hdev->raw_q))) |
| 2406 | hci_send_frame(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | } |
| 2408 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2409 | /* ----- HCI RX task (incoming data processing) ----- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | |
| 2411 | /* ACL data packet */ |
| 2412 | static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
| 2413 | { |
| 2414 | struct hci_acl_hdr *hdr = (void *) skb->data; |
| 2415 | struct hci_conn *conn; |
| 2416 | __u16 handle, flags; |
| 2417 | |
| 2418 | skb_pull(skb, HCI_ACL_HDR_SIZE); |
| 2419 | |
| 2420 | handle = __le16_to_cpu(hdr->handle); |
| 2421 | flags = hci_flags(handle); |
| 2422 | handle = hci_handle(handle); |
| 2423 | |
| 2424 | BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags); |
| 2425 | |
| 2426 | hdev->stat.acl_rx++; |
| 2427 | |
| 2428 | hci_dev_lock(hdev); |
| 2429 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 2430 | hci_dev_unlock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | if (conn) { |
Mat Martineau | 65983fc | 2011-12-13 15:06:02 -0800 | [diff] [blame] | 2433 | hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 2434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 2436 | l2cap_recv_acldata(conn, skb, flags); |
| 2437 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2439 | BT_ERR("%s ACL packet for unknown connection handle %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | hdev->name, handle); |
| 2441 | } |
| 2442 | |
| 2443 | kfree_skb(skb); |
| 2444 | } |
| 2445 | |
| 2446 | /* SCO data packet */ |
| 2447 | static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
| 2448 | { |
| 2449 | struct hci_sco_hdr *hdr = (void *) skb->data; |
| 2450 | struct hci_conn *conn; |
| 2451 | __u16 handle; |
| 2452 | |
| 2453 | skb_pull(skb, HCI_SCO_HDR_SIZE); |
| 2454 | |
| 2455 | handle = __le16_to_cpu(hdr->handle); |
| 2456 | |
| 2457 | BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle); |
| 2458 | |
| 2459 | hdev->stat.sco_rx++; |
| 2460 | |
| 2461 | hci_dev_lock(hdev); |
| 2462 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 2463 | hci_dev_unlock(hdev); |
| 2464 | |
| 2465 | if (conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 2467 | sco_recv_scodata(conn, skb); |
| 2468 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2470 | BT_ERR("%s SCO packet for unknown connection handle %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | hdev->name, handle); |
| 2472 | } |
| 2473 | |
| 2474 | kfree_skb(skb); |
| 2475 | } |
| 2476 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 2477 | static void hci_rx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | { |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 2479 | struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | struct sk_buff *skb; |
| 2481 | |
| 2482 | BT_DBG("%s", hdev->name); |
| 2483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | while ((skb = skb_dequeue(&hdev->rx_q))) { |
| 2485 | if (atomic_read(&hdev->promisc)) { |
| 2486 | /* Send copy to the sockets */ |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2487 | hci_send_to_sock(hdev, skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | if (test_bit(HCI_RAW, &hdev->flags)) { |
| 2491 | kfree_skb(skb); |
| 2492 | continue; |
| 2493 | } |
| 2494 | |
| 2495 | if (test_bit(HCI_INIT, &hdev->flags)) { |
| 2496 | /* Don't process data packets in this states. */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2497 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | case HCI_ACLDATA_PKT: |
| 2499 | case HCI_SCODATA_PKT: |
| 2500 | kfree_skb(skb); |
| 2501 | continue; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 2502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | /* Process frame */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2506 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | case HCI_EVENT_PKT: |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 2508 | BT_DBG("%s Event packet", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | hci_event_packet(hdev, skb); |
| 2510 | break; |
| 2511 | |
| 2512 | case HCI_ACLDATA_PKT: |
| 2513 | BT_DBG("%s ACL data packet", hdev->name); |
| 2514 | hci_acldata_packet(hdev, skb); |
| 2515 | break; |
| 2516 | |
| 2517 | case HCI_SCODATA_PKT: |
| 2518 | BT_DBG("%s SCO data packet", hdev->name); |
| 2519 | hci_scodata_packet(hdev, skb); |
| 2520 | break; |
| 2521 | |
| 2522 | default: |
| 2523 | kfree_skb(skb); |
| 2524 | break; |
| 2525 | } |
| 2526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2529 | static void hci_cmd_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | { |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2531 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | struct sk_buff *skb; |
| 2533 | |
| 2534 | BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt)); |
| 2535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | /* Send queued commands */ |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 2537 | if (atomic_read(&hdev->cmd_cnt)) { |
| 2538 | skb = skb_dequeue(&hdev->cmd_q); |
| 2539 | if (!skb) |
| 2540 | return; |
| 2541 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 2542 | kfree_skb(hdev->sent_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2544 | hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC); |
| 2545 | if (hdev->sent_cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | atomic_dec(&hdev->cmd_cnt); |
| 2547 | hci_send_frame(skb); |
Szymon Janc | 7bdb8a5 | 2011-07-26 22:46:54 +0200 | [diff] [blame] | 2548 | if (test_bit(HCI_RESET, &hdev->flags)) |
| 2549 | del_timer(&hdev->cmd_timer); |
| 2550 | else |
| 2551 | mod_timer(&hdev->cmd_timer, |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2552 | jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | } else { |
| 2554 | skb_queue_head(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2555 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | } |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 2559 | |
| 2560 | int hci_do_inquiry(struct hci_dev *hdev, u8 length) |
| 2561 | { |
| 2562 | /* General inquiry access code (GIAC) */ |
| 2563 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 2564 | struct hci_cp_inquiry cp; |
| 2565 | |
| 2566 | BT_DBG("%s", hdev->name); |
| 2567 | |
| 2568 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 2569 | return -EINPROGRESS; |
| 2570 | |
| 2571 | memset(&cp, 0, sizeof(cp)); |
| 2572 | memcpy(&cp.lap, lap, sizeof(cp.lap)); |
| 2573 | cp.length = length; |
| 2574 | |
| 2575 | return hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 2576 | } |
Andre Guedes | 023d5049 | 2011-11-04 14:16:52 -0300 | [diff] [blame] | 2577 | |
| 2578 | int hci_cancel_inquiry(struct hci_dev *hdev) |
| 2579 | { |
| 2580 | BT_DBG("%s", hdev->name); |
| 2581 | |
| 2582 | if (!test_bit(HCI_INQUIRY, &hdev->flags)) |
| 2583 | return -EPERM; |
| 2584 | |
| 2585 | return hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 2586 | } |
Andrei Emeltchenko | 7784d78 | 2011-11-18 13:35:42 +0200 | [diff] [blame] | 2587 | |
| 2588 | module_param(enable_hs, bool, 0644); |
| 2589 | MODULE_PARM_DESC(enable_hs, "Enable High Speed"); |