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