YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
Gustavo F. Padovan | 590051d | 2011-12-18 13:39:33 -0200 | [diff] [blame] | 4 | Copyright (C) 2011 ProFUSION Embedded Systems |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License version 2 as |
| 10 | published by the Free Software Foundation; |
| 11 | |
| 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 13 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 15 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 16 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 17 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 18 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 20 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 21 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 22 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | SOFTWARE IS DISCLAIMED. |
| 24 | */ |
| 25 | |
| 26 | /* Bluetooth HCI core. */ |
| 27 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 28 | #include <linux/export.h> |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 29 | #include <linux/idr.h> |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 30 | #include <linux/rfkill.h> |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 31 | #include <linux/debugfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <net/bluetooth/bluetooth.h> |
| 34 | #include <net/bluetooth/hci_core.h> |
| 35 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 36 | static void hci_rx_work(struct work_struct *work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 37 | static void hci_cmd_work(struct work_struct *work); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 38 | static void hci_tx_work(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* HCI device list */ |
| 41 | LIST_HEAD(hci_dev_list); |
| 42 | DEFINE_RWLOCK(hci_dev_list_lock); |
| 43 | |
| 44 | /* HCI callback list */ |
| 45 | LIST_HEAD(hci_cb_list); |
| 46 | DEFINE_RWLOCK(hci_cb_list_lock); |
| 47 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 48 | /* HCI ID Numbering */ |
| 49 | static DEFINE_IDA(hci_index_ida); |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* ---- HCI notifications ---- */ |
| 52 | |
Marcel Holtmann | 6516455 | 2005-10-28 19:20:48 +0200 | [diff] [blame] | 53 | static void hci_notify(struct hci_dev *hdev, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 55 | hci_sock_dev_event(hdev, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 58 | /* ---- HCI debugfs entries ---- */ |
| 59 | |
| 60 | static int inquiry_cache_show(struct seq_file *f, void *p) |
| 61 | { |
| 62 | struct hci_dev *hdev = f->private; |
| 63 | struct discovery_state *cache = &hdev->discovery; |
| 64 | struct inquiry_entry *e; |
| 65 | |
| 66 | hci_dev_lock(hdev); |
| 67 | |
| 68 | list_for_each_entry(e, &cache->all, all) { |
| 69 | struct inquiry_data *data = &e->data; |
| 70 | seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n", |
| 71 | &data->bdaddr, |
| 72 | data->pscan_rep_mode, data->pscan_period_mode, |
| 73 | data->pscan_mode, data->dev_class[2], |
| 74 | data->dev_class[1], data->dev_class[0], |
| 75 | __le16_to_cpu(data->clock_offset), |
| 76 | data->rssi, data->ssp_mode, e->timestamp); |
| 77 | } |
| 78 | |
| 79 | hci_dev_unlock(hdev); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int inquiry_cache_open(struct inode *inode, struct file *file) |
| 85 | { |
| 86 | return single_open(file, inquiry_cache_show, inode->i_private); |
| 87 | } |
| 88 | |
| 89 | static const struct file_operations inquiry_cache_fops = { |
| 90 | .open = inquiry_cache_open, |
| 91 | .read = seq_read, |
| 92 | .llseek = seq_lseek, |
| 93 | .release = single_release, |
| 94 | }; |
| 95 | |
Marcel Holtmann | ebd1e33 | 2013-10-17 10:54:46 -0700 | [diff] [blame^] | 96 | static int auto_accept_delay_set(void *data, u64 val) |
| 97 | { |
| 98 | struct hci_dev *hdev = data; |
| 99 | |
| 100 | hci_dev_lock(hdev); |
| 101 | hdev->auto_accept_delay = val; |
| 102 | hci_dev_unlock(hdev); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int auto_accept_delay_get(void *data, u64 *val) |
| 108 | { |
| 109 | struct hci_dev *hdev = data; |
| 110 | |
| 111 | hci_dev_lock(hdev); |
| 112 | *val = hdev->auto_accept_delay; |
| 113 | hci_dev_unlock(hdev); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get, |
| 119 | auto_accept_delay_set, "%llu\n"); |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* ---- HCI requests ---- */ |
| 122 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 123 | static void hci_req_sync_complete(struct hci_dev *hdev, u8 result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 125 | BT_DBG("%s result 0x%2.2x", hdev->name, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | if (hdev->req_status == HCI_REQ_PEND) { |
| 128 | hdev->req_result = result; |
| 129 | hdev->req_status = HCI_REQ_DONE; |
| 130 | wake_up_interruptible(&hdev->req_wait_q); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void hci_req_cancel(struct hci_dev *hdev, int err) |
| 135 | { |
| 136 | BT_DBG("%s err 0x%2.2x", hdev->name, err); |
| 137 | |
| 138 | if (hdev->req_status == HCI_REQ_PEND) { |
| 139 | hdev->req_result = err; |
| 140 | hdev->req_status = HCI_REQ_CANCELED; |
| 141 | wake_up_interruptible(&hdev->req_wait_q); |
| 142 | } |
| 143 | } |
| 144 | |
Fengguang Wu | 77a63e0 | 2013-04-20 16:24:31 +0300 | [diff] [blame] | 145 | static struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode, |
| 146 | u8 event) |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 147 | { |
| 148 | struct hci_ev_cmd_complete *ev; |
| 149 | struct hci_event_hdr *hdr; |
| 150 | struct sk_buff *skb; |
| 151 | |
| 152 | hci_dev_lock(hdev); |
| 153 | |
| 154 | skb = hdev->recv_evt; |
| 155 | hdev->recv_evt = NULL; |
| 156 | |
| 157 | hci_dev_unlock(hdev); |
| 158 | |
| 159 | if (!skb) |
| 160 | return ERR_PTR(-ENODATA); |
| 161 | |
| 162 | if (skb->len < sizeof(*hdr)) { |
| 163 | BT_ERR("Too short HCI event"); |
| 164 | goto failed; |
| 165 | } |
| 166 | |
| 167 | hdr = (void *) skb->data; |
| 168 | skb_pull(skb, HCI_EVENT_HDR_SIZE); |
| 169 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 170 | if (event) { |
| 171 | if (hdr->evt != event) |
| 172 | goto failed; |
| 173 | return skb; |
| 174 | } |
| 175 | |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 176 | if (hdr->evt != HCI_EV_CMD_COMPLETE) { |
| 177 | BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt); |
| 178 | goto failed; |
| 179 | } |
| 180 | |
| 181 | if (skb->len < sizeof(*ev)) { |
| 182 | BT_ERR("Too short cmd_complete event"); |
| 183 | goto failed; |
| 184 | } |
| 185 | |
| 186 | ev = (void *) skb->data; |
| 187 | skb_pull(skb, sizeof(*ev)); |
| 188 | |
| 189 | if (opcode == __le16_to_cpu(ev->opcode)) |
| 190 | return skb; |
| 191 | |
| 192 | BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode, |
| 193 | __le16_to_cpu(ev->opcode)); |
| 194 | |
| 195 | failed: |
| 196 | kfree_skb(skb); |
| 197 | return ERR_PTR(-ENODATA); |
| 198 | } |
| 199 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 200 | struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 201 | const void *param, u8 event, u32 timeout) |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 202 | { |
| 203 | DECLARE_WAITQUEUE(wait, current); |
| 204 | struct hci_request req; |
| 205 | int err = 0; |
| 206 | |
| 207 | BT_DBG("%s", hdev->name); |
| 208 | |
| 209 | hci_req_init(&req, hdev); |
| 210 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 211 | hci_req_add_ev(&req, opcode, plen, param, event); |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 212 | |
| 213 | hdev->req_status = HCI_REQ_PEND; |
| 214 | |
| 215 | err = hci_req_run(&req, hci_req_sync_complete); |
| 216 | if (err < 0) |
| 217 | return ERR_PTR(err); |
| 218 | |
| 219 | add_wait_queue(&hdev->req_wait_q, &wait); |
| 220 | set_current_state(TASK_INTERRUPTIBLE); |
| 221 | |
| 222 | schedule_timeout(timeout); |
| 223 | |
| 224 | remove_wait_queue(&hdev->req_wait_q, &wait); |
| 225 | |
| 226 | if (signal_pending(current)) |
| 227 | return ERR_PTR(-EINTR); |
| 228 | |
| 229 | switch (hdev->req_status) { |
| 230 | case HCI_REQ_DONE: |
| 231 | err = -bt_to_errno(hdev->req_result); |
| 232 | break; |
| 233 | |
| 234 | case HCI_REQ_CANCELED: |
| 235 | err = -hdev->req_result; |
| 236 | break; |
| 237 | |
| 238 | default: |
| 239 | err = -ETIMEDOUT; |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | hdev->req_status = hdev->req_result = 0; |
| 244 | |
| 245 | BT_DBG("%s end: err %d", hdev->name, err); |
| 246 | |
| 247 | if (err < 0) |
| 248 | return ERR_PTR(err); |
| 249 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 250 | return hci_get_cmd_complete(hdev, opcode, event); |
| 251 | } |
| 252 | EXPORT_SYMBOL(__hci_cmd_sync_ev); |
| 253 | |
| 254 | struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 255 | const void *param, u32 timeout) |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 256 | { |
| 257 | return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout); |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 258 | } |
| 259 | EXPORT_SYMBOL(__hci_cmd_sync); |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | /* Execute request and wait for completion. */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 262 | static int __hci_req_sync(struct hci_dev *hdev, |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 263 | void (*func)(struct hci_request *req, |
| 264 | unsigned long opt), |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 265 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 267 | struct hci_request req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | DECLARE_WAITQUEUE(wait, current); |
| 269 | int err = 0; |
| 270 | |
| 271 | BT_DBG("%s start", hdev->name); |
| 272 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 273 | hci_req_init(&req, hdev); |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | hdev->req_status = HCI_REQ_PEND; |
| 276 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 277 | func(&req, opt); |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 278 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 279 | err = hci_req_run(&req, hci_req_sync_complete); |
| 280 | if (err < 0) { |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 281 | hdev->req_status = 0; |
Andre Guedes | 920c830 | 2013-03-08 11:20:15 -0300 | [diff] [blame] | 282 | |
| 283 | /* ENODATA means the HCI request command queue is empty. |
| 284 | * This can happen when a request with conditionals doesn't |
| 285 | * trigger any commands to be sent. This is normal behavior |
| 286 | * and should not trigger an error return. |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 287 | */ |
Andre Guedes | 920c830 | 2013-03-08 11:20:15 -0300 | [diff] [blame] | 288 | if (err == -ENODATA) |
| 289 | return 0; |
| 290 | |
| 291 | return err; |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Andre Guedes | bc4445c | 2013-03-08 11:20:13 -0300 | [diff] [blame] | 294 | add_wait_queue(&hdev->req_wait_q, &wait); |
| 295 | set_current_state(TASK_INTERRUPTIBLE); |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | schedule_timeout(timeout); |
| 298 | |
| 299 | remove_wait_queue(&hdev->req_wait_q, &wait); |
| 300 | |
| 301 | if (signal_pending(current)) |
| 302 | return -EINTR; |
| 303 | |
| 304 | switch (hdev->req_status) { |
| 305 | case HCI_REQ_DONE: |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 306 | err = -bt_to_errno(hdev->req_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | break; |
| 308 | |
| 309 | case HCI_REQ_CANCELED: |
| 310 | err = -hdev->req_result; |
| 311 | break; |
| 312 | |
| 313 | default: |
| 314 | err = -ETIMEDOUT; |
| 315 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 318 | hdev->req_status = hdev->req_result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | BT_DBG("%s end: err %d", hdev->name, err); |
| 321 | |
| 322 | return err; |
| 323 | } |
| 324 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 325 | static int hci_req_sync(struct hci_dev *hdev, |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 326 | void (*req)(struct hci_request *req, |
| 327 | unsigned long opt), |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 328 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | int ret; |
| 331 | |
Marcel Holtmann | 7c6a329 | 2008-09-12 03:11:54 +0200 | [diff] [blame] | 332 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 333 | return -ENETDOWN; |
| 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | /* Serialize all requests */ |
| 336 | hci_req_lock(hdev); |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 337 | ret = __hci_req_sync(hdev, req, opt, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | hci_req_unlock(hdev); |
| 339 | |
| 340 | return ret; |
| 341 | } |
| 342 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 343 | static void hci_reset_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 345 | BT_DBG("%s %ld", req->hdev->name, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | /* Reset device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 348 | set_bit(HCI_RESET, &req->hdev->flags); |
| 349 | hci_req_add(req, HCI_OP_RESET, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 352 | static void bredr_init(struct hci_request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 354 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | /* Read Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 357 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Marcel Holtmann | 1143e5a | 2006-09-23 09:57:20 +0200 | [diff] [blame] | 359 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 360 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 361 | |
| 362 | /* Read BD Address */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 363 | hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 366 | static void amp_init(struct hci_request *req) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 367 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 368 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 369 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 370 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 371 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 372 | |
Marcel Holtmann | f6996cf | 2013-10-07 02:31:39 -0700 | [diff] [blame] | 373 | /* Read Local Supported Commands */ |
| 374 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
| 375 | |
| 376 | /* Read Local Supported Features */ |
| 377 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
| 378 | |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 379 | /* Read Local AMP Info */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 380 | hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); |
Andrei Emeltchenko | e71dfab | 2012-09-06 15:05:46 +0300 | [diff] [blame] | 381 | |
| 382 | /* Read Data Blk size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 383 | hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); |
Marcel Holtmann | 7528ca1 | 2013-10-07 03:55:52 -0700 | [diff] [blame] | 384 | |
Marcel Holtmann | f38ba94 | 2013-10-07 03:55:53 -0700 | [diff] [blame] | 385 | /* Read Flow Control Mode */ |
| 386 | hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL); |
| 387 | |
Marcel Holtmann | 7528ca1 | 2013-10-07 03:55:52 -0700 | [diff] [blame] | 388 | /* Read Location Data */ |
| 389 | hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 392 | static void hci_init1_req(struct hci_request *req, unsigned long opt) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 393 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 394 | struct hci_dev *hdev = req->hdev; |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 395 | |
| 396 | BT_DBG("%s %ld", hdev->name, opt); |
| 397 | |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 398 | /* Reset */ |
| 399 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 400 | hci_reset_req(req, 0); |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 401 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 402 | switch (hdev->dev_type) { |
| 403 | case HCI_BREDR: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 404 | bredr_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 405 | break; |
| 406 | |
| 407 | case HCI_AMP: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 408 | amp_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 409 | break; |
| 410 | |
| 411 | default: |
| 412 | BT_ERR("Unknown device type %d", hdev->dev_type); |
| 413 | break; |
| 414 | } |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 417 | static void bredr_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 418 | { |
Marcel Holtmann | 4ca048e | 2013-10-11 16:42:07 -0700 | [diff] [blame] | 419 | struct hci_dev *hdev = req->hdev; |
| 420 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 421 | __le16 param; |
| 422 | __u8 flt_type; |
| 423 | |
| 424 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 425 | hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 426 | |
| 427 | /* Read Class of Device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 428 | hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 429 | |
| 430 | /* Read Local Name */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 431 | hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 432 | |
| 433 | /* Read Voice Setting */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 434 | hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 435 | |
Marcel Holtmann | b4cb9fb | 2013-10-14 13:56:16 -0700 | [diff] [blame] | 436 | /* Read Number of Supported IAC */ |
| 437 | hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL); |
| 438 | |
Marcel Holtmann | 4b836f3 | 2013-10-14 14:06:36 -0700 | [diff] [blame] | 439 | /* Read Current IAC LAP */ |
| 440 | hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL); |
| 441 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 442 | /* Clear Event Filters */ |
| 443 | flt_type = HCI_FLT_CLEAR_ALL; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 444 | hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 445 | |
| 446 | /* Connection accept timeout ~20 secs */ |
| 447 | param = __constant_cpu_to_le16(0x7d00); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 448 | hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 449 | |
Marcel Holtmann | 4ca048e | 2013-10-11 16:42:07 -0700 | [diff] [blame] | 450 | /* AVM Berlin (31), aka "BlueFRITZ!", reports version 1.2, |
| 451 | * but it does not support page scan related HCI commands. |
| 452 | */ |
| 453 | if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) { |
Johan Hedberg | f332ec6 | 2013-03-15 17:07:11 -0500 | [diff] [blame] | 454 | hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL); |
| 455 | hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL); |
| 456 | } |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 459 | static void le_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 460 | { |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 461 | struct hci_dev *hdev = req->hdev; |
| 462 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 463 | /* Read LE Buffer Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 464 | hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 465 | |
| 466 | /* Read LE Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 467 | hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 468 | |
| 469 | /* Read LE Advertising Channel TX Power */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 470 | hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 471 | |
| 472 | /* Read LE White List Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 473 | hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 474 | |
| 475 | /* Read LE Supported States */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 476 | hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL); |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 477 | |
| 478 | /* LE-only controllers have LE implicitly enabled */ |
| 479 | if (!lmp_bredr_capable(hdev)) |
| 480 | set_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static u8 hci_get_inquiry_mode(struct hci_dev *hdev) |
| 484 | { |
| 485 | if (lmp_ext_inq_capable(hdev)) |
| 486 | return 0x02; |
| 487 | |
| 488 | if (lmp_inq_rssi_capable(hdev)) |
| 489 | return 0x01; |
| 490 | |
| 491 | if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 && |
| 492 | hdev->lmp_subver == 0x0757) |
| 493 | return 0x01; |
| 494 | |
| 495 | if (hdev->manufacturer == 15) { |
| 496 | if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963) |
| 497 | return 0x01; |
| 498 | if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963) |
| 499 | return 0x01; |
| 500 | if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965) |
| 501 | return 0x01; |
| 502 | } |
| 503 | |
| 504 | if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 && |
| 505 | hdev->lmp_subver == 0x1805) |
| 506 | return 0x01; |
| 507 | |
| 508 | return 0x00; |
| 509 | } |
| 510 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 511 | static void hci_setup_inquiry_mode(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 512 | { |
| 513 | u8 mode; |
| 514 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 515 | mode = hci_get_inquiry_mode(req->hdev); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 516 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 517 | hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 520 | static void hci_setup_event_mask(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 521 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 522 | struct hci_dev *hdev = req->hdev; |
| 523 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 524 | /* The second byte is 0xff instead of 0x9f (two reserved bits |
| 525 | * disabled) since a Broadcom 1.2 dongle doesn't respond to the |
| 526 | * command otherwise. |
| 527 | */ |
| 528 | u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; |
| 529 | |
| 530 | /* CSR 1.1 dongles does not accept any bitfield so don't try to set |
| 531 | * any event mask for pre 1.2 devices. |
| 532 | */ |
| 533 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 534 | return; |
| 535 | |
| 536 | if (lmp_bredr_capable(hdev)) { |
| 537 | events[4] |= 0x01; /* Flow Specification Complete */ |
| 538 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 539 | events[4] |= 0x04; /* Read Remote Extended Features Complete */ |
| 540 | events[5] |= 0x08; /* Synchronous Connection Complete */ |
| 541 | events[5] |= 0x10; /* Synchronous Connection Changed */ |
Marcel Holtmann | c7882cb | 2013-08-13 10:00:54 -0700 | [diff] [blame] | 542 | } else { |
| 543 | /* Use a different default for LE-only devices */ |
| 544 | memset(events, 0, sizeof(events)); |
| 545 | events[0] |= 0x10; /* Disconnection Complete */ |
| 546 | events[0] |= 0x80; /* Encryption Change */ |
| 547 | events[1] |= 0x08; /* Read Remote Version Information Complete */ |
| 548 | events[1] |= 0x20; /* Command Complete */ |
| 549 | events[1] |= 0x40; /* Command Status */ |
| 550 | events[1] |= 0x80; /* Hardware Error */ |
| 551 | events[2] |= 0x04; /* Number of Completed Packets */ |
| 552 | events[3] |= 0x02; /* Data Buffer Overflow */ |
| 553 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | if (lmp_inq_rssi_capable(hdev)) |
| 557 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 558 | |
| 559 | if (lmp_sniffsubr_capable(hdev)) |
| 560 | events[5] |= 0x20; /* Sniff Subrating */ |
| 561 | |
| 562 | if (lmp_pause_enc_capable(hdev)) |
| 563 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
| 564 | |
| 565 | if (lmp_ext_inq_capable(hdev)) |
| 566 | events[5] |= 0x40; /* Extended Inquiry Result */ |
| 567 | |
| 568 | if (lmp_no_flush_capable(hdev)) |
| 569 | events[7] |= 0x01; /* Enhanced Flush Complete */ |
| 570 | |
| 571 | if (lmp_lsto_capable(hdev)) |
| 572 | events[6] |= 0x80; /* Link Supervision Timeout Changed */ |
| 573 | |
| 574 | if (lmp_ssp_capable(hdev)) { |
| 575 | events[6] |= 0x01; /* IO Capability Request */ |
| 576 | events[6] |= 0x02; /* IO Capability Response */ |
| 577 | events[6] |= 0x04; /* User Confirmation Request */ |
| 578 | events[6] |= 0x08; /* User Passkey Request */ |
| 579 | events[6] |= 0x10; /* Remote OOB Data Request */ |
| 580 | events[6] |= 0x20; /* Simple Pairing Complete */ |
| 581 | events[7] |= 0x04; /* User Passkey Notification */ |
| 582 | events[7] |= 0x08; /* Keypress Notification */ |
| 583 | events[7] |= 0x10; /* Remote Host Supported |
| 584 | * Features Notification |
| 585 | */ |
| 586 | } |
| 587 | |
| 588 | if (lmp_le_capable(hdev)) |
| 589 | events[7] |= 0x20; /* LE Meta-Event */ |
| 590 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 591 | hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 592 | |
| 593 | if (lmp_le_capable(hdev)) { |
| 594 | memset(events, 0, sizeof(events)); |
| 595 | events[0] = 0x1f; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 596 | hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, |
| 597 | sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 601 | static void hci_init2_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 602 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 603 | struct hci_dev *hdev = req->hdev; |
| 604 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 605 | if (lmp_bredr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 606 | bredr_setup(req); |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 607 | else |
| 608 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 609 | |
| 610 | if (lmp_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 611 | le_setup(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 612 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 613 | hci_setup_event_mask(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 614 | |
Johan Hedberg | 3f8e2d7 | 2013-07-24 02:32:46 +0300 | [diff] [blame] | 615 | /* AVM Berlin (31), aka "BlueFRITZ!", doesn't support the read |
| 616 | * local supported commands HCI command. |
| 617 | */ |
| 618 | if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 619 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 620 | |
| 621 | if (lmp_ssp_capable(hdev)) { |
| 622 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 623 | u8 mode = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 624 | hci_req_add(req, HCI_OP_WRITE_SSP_MODE, |
| 625 | sizeof(mode), &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 626 | } else { |
| 627 | struct hci_cp_write_eir cp; |
| 628 | |
| 629 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 630 | memset(&cp, 0, sizeof(cp)); |
| 631 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 632 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
| 636 | if (lmp_inq_rssi_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 637 | hci_setup_inquiry_mode(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 638 | |
| 639 | if (lmp_inq_tx_pwr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 640 | hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 641 | |
| 642 | if (lmp_ext_feat_capable(hdev)) { |
| 643 | struct hci_cp_read_local_ext_features cp; |
| 644 | |
| 645 | cp.page = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 646 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 647 | sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) { |
| 651 | u8 enable = 1; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 652 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), |
| 653 | &enable); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 657 | static void hci_setup_link_policy(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 658 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 659 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 660 | struct hci_cp_write_def_link_policy cp; |
| 661 | u16 link_policy = 0; |
| 662 | |
| 663 | if (lmp_rswitch_capable(hdev)) |
| 664 | link_policy |= HCI_LP_RSWITCH; |
| 665 | if (lmp_hold_capable(hdev)) |
| 666 | link_policy |= HCI_LP_HOLD; |
| 667 | if (lmp_sniff_capable(hdev)) |
| 668 | link_policy |= HCI_LP_SNIFF; |
| 669 | if (lmp_park_capable(hdev)) |
| 670 | link_policy |= HCI_LP_PARK; |
| 671 | |
| 672 | cp.policy = cpu_to_le16(link_policy); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 673 | hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 676 | static void hci_set_le_support(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 677 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 678 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 679 | struct hci_cp_write_le_host_supported cp; |
| 680 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 681 | /* LE-only devices do not support explicit enablement */ |
| 682 | if (!lmp_bredr_capable(hdev)) |
| 683 | return; |
| 684 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 685 | memset(&cp, 0, sizeof(cp)); |
| 686 | |
| 687 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 688 | cp.le = 0x01; |
| 689 | cp.simul = lmp_le_br_capable(hdev); |
| 690 | } |
| 691 | |
| 692 | if (cp.le != lmp_host_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 693 | hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp), |
| 694 | &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 697 | static void hci_set_event_mask_page_2(struct hci_request *req) |
| 698 | { |
| 699 | struct hci_dev *hdev = req->hdev; |
| 700 | u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 701 | |
| 702 | /* If Connectionless Slave Broadcast master role is supported |
| 703 | * enable all necessary events for it. |
| 704 | */ |
| 705 | if (hdev->features[2][0] & 0x01) { |
| 706 | events[1] |= 0x40; /* Triggered Clock Capture */ |
| 707 | events[1] |= 0x80; /* Synchronization Train Complete */ |
| 708 | events[2] |= 0x10; /* Slave Page Response Timeout */ |
| 709 | events[2] |= 0x20; /* CSB Channel Map Change */ |
| 710 | } |
| 711 | |
| 712 | /* If Connectionless Slave Broadcast slave role is supported |
| 713 | * enable all necessary events for it. |
| 714 | */ |
| 715 | if (hdev->features[2][0] & 0x02) { |
| 716 | events[2] |= 0x01; /* Synchronization Train Received */ |
| 717 | events[2] |= 0x02; /* CSB Receive */ |
| 718 | events[2] |= 0x04; /* CSB Timeout */ |
| 719 | events[2] |= 0x08; /* Truncated Page Complete */ |
| 720 | } |
| 721 | |
| 722 | hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events); |
| 723 | } |
| 724 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 725 | static void hci_init3_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 726 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 727 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | d2c5d77 | 2013-04-17 15:00:52 +0300 | [diff] [blame] | 728 | u8 p; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 729 | |
Gustavo Padovan | b8f4e06 | 2013-06-13 12:34:31 +0100 | [diff] [blame] | 730 | /* Some Broadcom based Bluetooth controllers do not support the |
| 731 | * Delete Stored Link Key command. They are clearly indicating its |
| 732 | * absence in the bit mask of supported commands. |
| 733 | * |
| 734 | * Check the supported commands and only if the the command is marked |
| 735 | * as supported send it. If not supported assume that the controller |
| 736 | * does not have actual support for stored link keys which makes this |
| 737 | * command redundant anyway. |
Marcel Holtmann | 637b4ca | 2013-07-01 14:14:46 -0700 | [diff] [blame] | 738 | */ |
Johan Hedberg | 59f45d5 | 2013-06-13 11:01:13 +0300 | [diff] [blame] | 739 | if (hdev->commands[6] & 0x80) { |
| 740 | struct hci_cp_delete_stored_link_key cp; |
| 741 | |
| 742 | bacpy(&cp.bdaddr, BDADDR_ANY); |
| 743 | cp.delete_all = 0x01; |
| 744 | hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY, |
| 745 | sizeof(cp), &cp); |
| 746 | } |
| 747 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 748 | if (hdev->commands[5] & 0x10) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 749 | hci_setup_link_policy(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 750 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 751 | if (lmp_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 752 | hci_set_le_support(req); |
Johan Hedberg | d2c5d77 | 2013-04-17 15:00:52 +0300 | [diff] [blame] | 753 | |
| 754 | /* Read features beyond page 1 if available */ |
| 755 | for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { |
| 756 | struct hci_cp_read_local_ext_features cp; |
| 757 | |
| 758 | cp.page = p; |
| 759 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 760 | sizeof(cp), &cp); |
| 761 | } |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 764 | static void hci_init4_req(struct hci_request *req, unsigned long opt) |
| 765 | { |
| 766 | struct hci_dev *hdev = req->hdev; |
| 767 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 768 | /* Set event mask page 2 if the HCI command for it is supported */ |
| 769 | if (hdev->commands[22] & 0x04) |
| 770 | hci_set_event_mask_page_2(req); |
| 771 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 772 | /* Check for Synchronization Train support */ |
| 773 | if (hdev->features[2][0] & 0x04) |
| 774 | hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL); |
| 775 | } |
| 776 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 777 | static int __hci_init(struct hci_dev *hdev) |
| 778 | { |
| 779 | int err; |
| 780 | |
| 781 | err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT); |
| 782 | if (err < 0) |
| 783 | return err; |
| 784 | |
| 785 | /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode |
| 786 | * BR/EDR/LE type controllers. AMP controllers only need the |
| 787 | * first stage init. |
| 788 | */ |
| 789 | if (hdev->dev_type != HCI_BREDR) |
| 790 | return 0; |
| 791 | |
| 792 | err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT); |
| 793 | if (err < 0) |
| 794 | return err; |
| 795 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 796 | err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); |
| 797 | if (err < 0) |
| 798 | return err; |
| 799 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 800 | err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT); |
| 801 | if (err < 0) |
| 802 | return err; |
| 803 | |
| 804 | /* Only create debugfs entries during the initial setup |
| 805 | * phase and not every time the controller gets powered on. |
| 806 | */ |
| 807 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 808 | return 0; |
| 809 | |
| 810 | if (lmp_bredr_capable(hdev)) { |
| 811 | debugfs_create_file("inquiry_cache", 0444, hdev->debugfs, |
| 812 | hdev, &inquiry_cache_fops); |
| 813 | } |
| 814 | |
Marcel Holtmann | ebd1e33 | 2013-10-17 10:54:46 -0700 | [diff] [blame^] | 815 | if (lmp_ssp_capable(hdev)) |
| 816 | debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs, |
| 817 | hdev, &auto_accept_delay_fops); |
| 818 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 819 | return 0; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 822 | static void hci_scan_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
| 824 | __u8 scan = opt; |
| 825 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 826 | BT_DBG("%s %x", req->hdev->name, scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
| 828 | /* Inquiry and Page scans */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 829 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 832 | static void hci_auth_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
| 834 | __u8 auth = opt; |
| 835 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 836 | BT_DBG("%s %x", req->hdev->name, auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
| 838 | /* Authentication */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 839 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 842 | static void hci_encrypt_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | { |
| 844 | __u8 encrypt = opt; |
| 845 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 846 | BT_DBG("%s %x", req->hdev->name, encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 848 | /* Encryption */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 849 | hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 852 | static void hci_linkpol_req(struct hci_request *req, unsigned long opt) |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 853 | { |
| 854 | __le16 policy = cpu_to_le16(opt); |
| 855 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 856 | BT_DBG("%s %x", req->hdev->name, policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 857 | |
| 858 | /* Default link policy */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 859 | hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 860 | } |
| 861 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 862 | /* Get HCI device by index. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | * Device is held on return. */ |
| 864 | struct hci_dev *hci_dev_get(int index) |
| 865 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 866 | struct hci_dev *hdev = NULL, *d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | BT_DBG("%d", index); |
| 869 | |
| 870 | if (index < 0) |
| 871 | return NULL; |
| 872 | |
| 873 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 874 | list_for_each_entry(d, &hci_dev_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | if (d->id == index) { |
| 876 | hdev = hci_dev_hold(d); |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | read_unlock(&hci_dev_list_lock); |
| 881 | return hdev; |
| 882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
| 884 | /* ---- Inquiry support ---- */ |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 885 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 886 | bool hci_discovery_active(struct hci_dev *hdev) |
| 887 | { |
| 888 | struct discovery_state *discov = &hdev->discovery; |
| 889 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 890 | switch (discov->state) { |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 891 | case DISCOVERY_FINDING: |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 892 | case DISCOVERY_RESOLVING: |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 893 | return true; |
| 894 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 895 | default: |
| 896 | return false; |
| 897 | } |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 900 | void hci_discovery_set_state(struct hci_dev *hdev, int state) |
| 901 | { |
| 902 | BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); |
| 903 | |
| 904 | if (hdev->discovery.state == state) |
| 905 | return; |
| 906 | |
| 907 | switch (state) { |
| 908 | case DISCOVERY_STOPPED: |
Andre Guedes | 7b99b65 | 2012-02-13 15:41:02 -0300 | [diff] [blame] | 909 | if (hdev->discovery.state != DISCOVERY_STARTING) |
| 910 | mgmt_discovering(hdev, 0); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 911 | break; |
| 912 | case DISCOVERY_STARTING: |
| 913 | break; |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 914 | case DISCOVERY_FINDING: |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 915 | mgmt_discovering(hdev, 1); |
| 916 | break; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 917 | case DISCOVERY_RESOLVING: |
| 918 | break; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 919 | case DISCOVERY_STOPPING: |
| 920 | break; |
| 921 | } |
| 922 | |
| 923 | hdev->discovery.state = state; |
| 924 | } |
| 925 | |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 926 | void hci_inquiry_cache_flush(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 928 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 929 | struct inquiry_entry *p, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 931 | list_for_each_entry_safe(p, n, &cache->all, all) { |
| 932 | list_del(&p->all); |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 933 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 935 | |
| 936 | INIT_LIST_HEAD(&cache->unknown); |
| 937 | INIT_LIST_HEAD(&cache->resolve); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 940 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, |
| 941 | bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 943 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | struct inquiry_entry *e; |
| 945 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 946 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 948 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 950 | return e; |
| 951 | } |
| 952 | |
| 953 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 956 | struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 957 | bdaddr_t *bdaddr) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 958 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 959 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 960 | struct inquiry_entry *e; |
| 961 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 962 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 963 | |
| 964 | list_for_each_entry(e, &cache->unknown, list) { |
| 965 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 966 | return e; |
| 967 | } |
| 968 | |
| 969 | return NULL; |
| 970 | } |
| 971 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 972 | struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 973 | bdaddr_t *bdaddr, |
| 974 | int state) |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 975 | { |
| 976 | struct discovery_state *cache = &hdev->discovery; |
| 977 | struct inquiry_entry *e; |
| 978 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 979 | BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 980 | |
| 981 | list_for_each_entry(e, &cache->resolve, list) { |
| 982 | if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) |
| 983 | return e; |
| 984 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 985 | return e; |
| 986 | } |
| 987 | |
| 988 | return NULL; |
| 989 | } |
| 990 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 991 | void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 992 | struct inquiry_entry *ie) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 993 | { |
| 994 | struct discovery_state *cache = &hdev->discovery; |
| 995 | struct list_head *pos = &cache->resolve; |
| 996 | struct inquiry_entry *p; |
| 997 | |
| 998 | list_del(&ie->list); |
| 999 | |
| 1000 | list_for_each_entry(p, &cache->resolve, list) { |
| 1001 | if (p->name_state != NAME_PENDING && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1002 | abs(p->data.rssi) >= abs(ie->data.rssi)) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1003 | break; |
| 1004 | pos = &p->list; |
| 1005 | } |
| 1006 | |
| 1007 | list_add(&ie->list, pos); |
| 1008 | } |
| 1009 | |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1010 | bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1011 | bool name_known, bool *ssp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1013 | struct discovery_state *cache = &hdev->discovery; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1014 | struct inquiry_entry *ie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1016 | BT_DBG("cache %p, %pMR", cache, &data->bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Szymon Janc | 2b2fec4 | 2012-11-20 11:38:54 +0100 | [diff] [blame] | 1018 | hci_remove_remote_oob_data(hdev, &data->bdaddr); |
| 1019 | |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 1020 | if (ssp) |
| 1021 | *ssp = data->ssp_mode; |
| 1022 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1023 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1024 | if (ie) { |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 1025 | if (ie->data.ssp_mode && ssp) |
| 1026 | *ssp = true; |
| 1027 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1028 | if (ie->name_state == NAME_NEEDED && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1029 | data->rssi != ie->data.rssi) { |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1030 | ie->data.rssi = data->rssi; |
| 1031 | hci_inquiry_cache_update_resolve(hdev, ie); |
| 1032 | } |
| 1033 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1034 | goto update; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1035 | } |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1036 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1037 | /* Entry not in the cache. Add new one. */ |
| 1038 | ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); |
| 1039 | if (!ie) |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1040 | return false; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1041 | |
| 1042 | list_add(&ie->all, &cache->all); |
| 1043 | |
| 1044 | if (name_known) { |
| 1045 | ie->name_state = NAME_KNOWN; |
| 1046 | } else { |
| 1047 | ie->name_state = NAME_NOT_KNOWN; |
| 1048 | list_add(&ie->list, &cache->unknown); |
| 1049 | } |
| 1050 | |
| 1051 | update: |
| 1052 | if (name_known && ie->name_state != NAME_KNOWN && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1053 | ie->name_state != NAME_PENDING) { |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1054 | ie->name_state = NAME_KNOWN; |
| 1055 | list_del(&ie->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1058 | memcpy(&ie->data, data, sizeof(*data)); |
| 1059 | ie->timestamp = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | cache->timestamp = jiffies; |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1061 | |
| 1062 | if (ie->name_state == NAME_NOT_KNOWN) |
| 1063 | return false; |
| 1064 | |
| 1065 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) |
| 1069 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1070 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | struct inquiry_info *info = (struct inquiry_info *) buf; |
| 1072 | struct inquiry_entry *e; |
| 1073 | int copied = 0; |
| 1074 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1075 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | struct inquiry_data *data = &e->data; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1077 | |
| 1078 | if (copied >= num) |
| 1079 | break; |
| 1080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | bacpy(&info->bdaddr, &data->bdaddr); |
| 1082 | info->pscan_rep_mode = data->pscan_rep_mode; |
| 1083 | info->pscan_period_mode = data->pscan_period_mode; |
| 1084 | info->pscan_mode = data->pscan_mode; |
| 1085 | memcpy(info->dev_class, data->dev_class, 3); |
| 1086 | info->clock_offset = data->clock_offset; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | info++; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1089 | copied++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | BT_DBG("cache %p, copied %d", cache, copied); |
| 1093 | return copied; |
| 1094 | } |
| 1095 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1096 | static void hci_inq_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | { |
| 1098 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1099 | struct hci_dev *hdev = req->hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | struct hci_cp_inquiry cp; |
| 1101 | |
| 1102 | BT_DBG("%s", hdev->name); |
| 1103 | |
| 1104 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 1105 | return; |
| 1106 | |
| 1107 | /* Start Inquiry */ |
| 1108 | memcpy(&cp.lap, &ir->lap, 3); |
| 1109 | cp.length = ir->length; |
| 1110 | cp.num_rsp = ir->num_rsp; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1111 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Andre Guedes | 3e13fa1 | 2013-03-27 20:04:56 -0300 | [diff] [blame] | 1114 | static int wait_inquiry(void *word) |
| 1115 | { |
| 1116 | schedule(); |
| 1117 | return signal_pending(current); |
| 1118 | } |
| 1119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | int hci_inquiry(void __user *arg) |
| 1121 | { |
| 1122 | __u8 __user *ptr = arg; |
| 1123 | struct hci_inquiry_req ir; |
| 1124 | struct hci_dev *hdev; |
| 1125 | int err = 0, do_inquiry = 0, max_rsp; |
| 1126 | long timeo; |
| 1127 | __u8 *buf; |
| 1128 | |
| 1129 | if (copy_from_user(&ir, ptr, sizeof(ir))) |
| 1130 | return -EFAULT; |
| 1131 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 1132 | hdev = hci_dev_get(ir.dev_id); |
| 1133 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | return -ENODEV; |
| 1135 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1136 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1137 | err = -EBUSY; |
| 1138 | goto done; |
| 1139 | } |
| 1140 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 1141 | if (hdev->dev_type != HCI_BREDR) { |
| 1142 | err = -EOPNOTSUPP; |
| 1143 | goto done; |
| 1144 | } |
| 1145 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1146 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1147 | err = -EOPNOTSUPP; |
| 1148 | goto done; |
| 1149 | } |
| 1150 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1151 | hci_dev_lock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1152 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1153 | inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1154 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | do_inquiry = 1; |
| 1156 | } |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1157 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1159 | timeo = ir.length * msecs_to_jiffies(2000); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1160 | |
| 1161 | if (do_inquiry) { |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1162 | err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir, |
| 1163 | timeo); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1164 | if (err < 0) |
| 1165 | goto done; |
Andre Guedes | 3e13fa1 | 2013-03-27 20:04:56 -0300 | [diff] [blame] | 1166 | |
| 1167 | /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is |
| 1168 | * cleared). If it is interrupted by a signal, return -EINTR. |
| 1169 | */ |
| 1170 | if (wait_on_bit(&hdev->flags, HCI_INQUIRY, wait_inquiry, |
| 1171 | TASK_INTERRUPTIBLE)) |
| 1172 | return -EINTR; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1175 | /* for unlimited number of responses we will use buffer with |
| 1176 | * 255 entries |
| 1177 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; |
| 1179 | |
| 1180 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then |
| 1181 | * copy it to the user space. |
| 1182 | */ |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 1183 | buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1184 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | err = -ENOMEM; |
| 1186 | goto done; |
| 1187 | } |
| 1188 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1189 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1191 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
| 1193 | BT_DBG("num_rsp %d", ir.num_rsp); |
| 1194 | |
| 1195 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { |
| 1196 | ptr += sizeof(ir); |
| 1197 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1198 | ir.num_rsp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | err = -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1200 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | err = -EFAULT; |
| 1202 | |
| 1203 | kfree(buf); |
| 1204 | |
| 1205 | done: |
| 1206 | hci_dev_put(hdev); |
| 1207 | return err; |
| 1208 | } |
| 1209 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1210 | static int hci_dev_do_open(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | int ret = 0; |
| 1213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | BT_DBG("%s %p", hdev->name, hdev); |
| 1215 | |
| 1216 | hci_req_lock(hdev); |
| 1217 | |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 1218 | if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { |
| 1219 | ret = -ENODEV; |
| 1220 | goto done; |
| 1221 | } |
| 1222 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1223 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) { |
| 1224 | /* Check for rfkill but allow the HCI setup stage to |
| 1225 | * proceed (which in itself doesn't cause any RF activity). |
| 1226 | */ |
| 1227 | if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) { |
| 1228 | ret = -ERFKILL; |
| 1229 | goto done; |
| 1230 | } |
| 1231 | |
| 1232 | /* Check for valid public address or a configured static |
| 1233 | * random adddress, but let the HCI setup proceed to |
| 1234 | * be able to determine if there is a public address |
| 1235 | * or not. |
| 1236 | * |
| 1237 | * This check is only valid for BR/EDR controllers |
| 1238 | * since AMP controllers do not have an address. |
| 1239 | */ |
| 1240 | if (hdev->dev_type == HCI_BREDR && |
| 1241 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && |
| 1242 | !bacmp(&hdev->static_addr, BDADDR_ANY)) { |
| 1243 | ret = -EADDRNOTAVAIL; |
| 1244 | goto done; |
| 1245 | } |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 1249 | ret = -EALREADY; |
| 1250 | goto done; |
| 1251 | } |
| 1252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | if (hdev->open(hdev)) { |
| 1254 | ret = -EIO; |
| 1255 | goto done; |
| 1256 | } |
| 1257 | |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1258 | atomic_set(&hdev->cmd_cnt, 1); |
| 1259 | set_bit(HCI_INIT, &hdev->flags); |
| 1260 | |
| 1261 | if (hdev->setup && test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 1262 | ret = hdev->setup(hdev); |
| 1263 | |
| 1264 | if (!ret) { |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1265 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 1266 | set_bit(HCI_RAW, &hdev->flags); |
| 1267 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1268 | if (!test_bit(HCI_RAW, &hdev->flags) && |
| 1269 | !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1270 | ret = __hci_init(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1273 | clear_bit(HCI_INIT, &hdev->flags); |
| 1274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | if (!ret) { |
| 1276 | hci_dev_hold(hdev); |
| 1277 | set_bit(HCI_UP, &hdev->flags); |
| 1278 | hci_notify(hdev, HCI_DEV_UP); |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 1279 | if (!test_bit(HCI_SETUP, &hdev->dev_flags) && |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1280 | !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 1281 | hdev->dev_type == HCI_BREDR) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1282 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1283 | mgmt_powered(hdev, 1); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1284 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1285 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1286 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | /* Init failed, cleanup */ |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1288 | flush_work(&hdev->tx_work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1289 | flush_work(&hdev->cmd_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1290 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
| 1292 | skb_queue_purge(&hdev->cmd_q); |
| 1293 | skb_queue_purge(&hdev->rx_q); |
| 1294 | |
| 1295 | if (hdev->flush) |
| 1296 | hdev->flush(hdev); |
| 1297 | |
| 1298 | if (hdev->sent_cmd) { |
| 1299 | kfree_skb(hdev->sent_cmd); |
| 1300 | hdev->sent_cmd = NULL; |
| 1301 | } |
| 1302 | |
| 1303 | hdev->close(hdev); |
| 1304 | hdev->flags = 0; |
| 1305 | } |
| 1306 | |
| 1307 | done: |
| 1308 | hci_req_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | return ret; |
| 1310 | } |
| 1311 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1312 | /* ---- HCI ioctl helpers ---- */ |
| 1313 | |
| 1314 | int hci_dev_open(__u16 dev) |
| 1315 | { |
| 1316 | struct hci_dev *hdev; |
| 1317 | int err; |
| 1318 | |
| 1319 | hdev = hci_dev_get(dev); |
| 1320 | if (!hdev) |
| 1321 | return -ENODEV; |
| 1322 | |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 1323 | /* We need to ensure that no other power on/off work is pending |
| 1324 | * before proceeding to call hci_dev_do_open. This is |
| 1325 | * particularly important if the setup procedure has not yet |
| 1326 | * completed. |
| 1327 | */ |
| 1328 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 1329 | cancel_delayed_work(&hdev->power_off); |
| 1330 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1331 | /* After this call it is guaranteed that the setup procedure |
| 1332 | * has finished. This means that error conditions like RFKILL |
| 1333 | * or no valid public or static random address apply. |
| 1334 | */ |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 1335 | flush_workqueue(hdev->req_workqueue); |
| 1336 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1337 | err = hci_dev_do_open(hdev); |
| 1338 | |
| 1339 | hci_dev_put(hdev); |
| 1340 | |
| 1341 | return err; |
| 1342 | } |
| 1343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | static int hci_dev_do_close(struct hci_dev *hdev) |
| 1345 | { |
| 1346 | BT_DBG("%s %p", hdev->name, hdev); |
| 1347 | |
Vinicius Costa Gomes | 78c04c0 | 2012-09-14 16:34:46 -0300 | [diff] [blame] | 1348 | cancel_delayed_work(&hdev->power_off); |
| 1349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | hci_req_cancel(hdev, ENODEV); |
| 1351 | hci_req_lock(hdev); |
| 1352 | |
| 1353 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 1354 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | hci_req_unlock(hdev); |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1359 | /* Flush RX and TX works */ |
| 1360 | flush_work(&hdev->tx_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1361 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1363 | if (hdev->discov_timeout > 0) { |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 1364 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1365 | hdev->discov_timeout = 0; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1366 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1367 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1370 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1371 | cancel_delayed_work(&hdev->service_cache); |
| 1372 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1373 | cancel_delayed_work_sync(&hdev->le_scan_disable); |
| 1374 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1375 | hci_dev_lock(hdev); |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1376 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1378 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
| 1380 | hci_notify(hdev, HCI_DEV_DOWN); |
| 1381 | |
| 1382 | if (hdev->flush) |
| 1383 | hdev->flush(hdev); |
| 1384 | |
| 1385 | /* Reset device */ |
| 1386 | skb_queue_purge(&hdev->cmd_q); |
| 1387 | atomic_set(&hdev->cmd_cnt, 1); |
Johan Hedberg | 8af5946 | 2012-02-03 21:29:40 +0200 | [diff] [blame] | 1388 | if (!test_bit(HCI_RAW, &hdev->flags) && |
Marcel Holtmann | 3a6afbd | 2013-10-11 09:44:12 -0700 | [diff] [blame] | 1389 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags) && |
Szymon Janc | a6c511c | 2012-05-23 12:35:46 +0200 | [diff] [blame] | 1390 | test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | set_bit(HCI_INIT, &hdev->flags); |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1392 | __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | clear_bit(HCI_INIT, &hdev->flags); |
| 1394 | } |
| 1395 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1396 | /* flush cmd work */ |
| 1397 | flush_work(&hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | |
| 1399 | /* Drop queues */ |
| 1400 | skb_queue_purge(&hdev->rx_q); |
| 1401 | skb_queue_purge(&hdev->cmd_q); |
| 1402 | skb_queue_purge(&hdev->raw_q); |
| 1403 | |
| 1404 | /* Drop last sent command */ |
| 1405 | if (hdev->sent_cmd) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 1406 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | kfree_skb(hdev->sent_cmd); |
| 1408 | hdev->sent_cmd = NULL; |
| 1409 | } |
| 1410 | |
Johan Hedberg | b6ddb63 | 2013-04-02 13:34:31 +0300 | [diff] [blame] | 1411 | kfree_skb(hdev->recv_evt); |
| 1412 | hdev->recv_evt = NULL; |
| 1413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | /* After this point our queues are empty |
| 1415 | * and no tasks are scheduled. */ |
| 1416 | hdev->close(hdev); |
| 1417 | |
Johan Hedberg | 35b973c | 2013-03-15 17:06:59 -0500 | [diff] [blame] | 1418 | /* Clear flags */ |
| 1419 | hdev->flags = 0; |
| 1420 | hdev->dev_flags &= ~HCI_PERSISTENT_MASK; |
| 1421 | |
Marcel Holtmann | 93c311a | 2013-10-07 00:58:33 -0700 | [diff] [blame] | 1422 | if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1423 | if (hdev->dev_type == HCI_BREDR) { |
| 1424 | hci_dev_lock(hdev); |
| 1425 | mgmt_powered(hdev, 0); |
| 1426 | hci_dev_unlock(hdev); |
| 1427 | } |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1428 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1429 | |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 1430 | /* Controller radio is available but is currently powered down */ |
Marcel Holtmann | 536619e | 2013-10-05 11:47:45 -0700 | [diff] [blame] | 1431 | hdev->amp_status = AMP_STATUS_POWERED_DOWN; |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 1432 | |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 1433 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
Johan Hedberg | 09b3c3f | 2012-02-22 22:01:41 +0200 | [diff] [blame] | 1434 | memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 1435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | hci_req_unlock(hdev); |
| 1437 | |
| 1438 | hci_dev_put(hdev); |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | int hci_dev_close(__u16 dev) |
| 1443 | { |
| 1444 | struct hci_dev *hdev; |
| 1445 | int err; |
| 1446 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1447 | hdev = hci_dev_get(dev); |
| 1448 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | return -ENODEV; |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1450 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1451 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1452 | err = -EBUSY; |
| 1453 | goto done; |
| 1454 | } |
| 1455 | |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1456 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 1457 | cancel_delayed_work(&hdev->power_off); |
| 1458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | err = hci_dev_do_close(hdev); |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1460 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1461 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | hci_dev_put(hdev); |
| 1463 | return err; |
| 1464 | } |
| 1465 | |
| 1466 | int hci_dev_reset(__u16 dev) |
| 1467 | { |
| 1468 | struct hci_dev *hdev; |
| 1469 | int ret = 0; |
| 1470 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1471 | hdev = hci_dev_get(dev); |
| 1472 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | return -ENODEV; |
| 1474 | |
| 1475 | hci_req_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Marcel Holtmann | 808a049 | 2013-08-26 20:57:58 -0700 | [diff] [blame] | 1477 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1478 | ret = -ENETDOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | goto done; |
Marcel Holtmann | 808a049 | 2013-08-26 20:57:58 -0700 | [diff] [blame] | 1480 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1482 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1483 | ret = -EBUSY; |
| 1484 | goto done; |
| 1485 | } |
| 1486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | /* Drop queues */ |
| 1488 | skb_queue_purge(&hdev->rx_q); |
| 1489 | skb_queue_purge(&hdev->cmd_q); |
| 1490 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1491 | hci_dev_lock(hdev); |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1492 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1494 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
| 1496 | if (hdev->flush) |
| 1497 | hdev->flush(hdev); |
| 1498 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1499 | atomic_set(&hdev->cmd_cnt, 1); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 1500 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | |
| 1502 | if (!test_bit(HCI_RAW, &hdev->flags)) |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1503 | ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
| 1505 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | hci_req_unlock(hdev); |
| 1507 | hci_dev_put(hdev); |
| 1508 | return ret; |
| 1509 | } |
| 1510 | |
| 1511 | int hci_dev_reset_stat(__u16 dev) |
| 1512 | { |
| 1513 | struct hci_dev *hdev; |
| 1514 | int ret = 0; |
| 1515 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1516 | hdev = hci_dev_get(dev); |
| 1517 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | return -ENODEV; |
| 1519 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1520 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1521 | ret = -EBUSY; |
| 1522 | goto done; |
| 1523 | } |
| 1524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 1526 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1527 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | hci_dev_put(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | return ret; |
| 1530 | } |
| 1531 | |
| 1532 | int hci_dev_cmd(unsigned int cmd, void __user *arg) |
| 1533 | { |
| 1534 | struct hci_dev *hdev; |
| 1535 | struct hci_dev_req dr; |
| 1536 | int err = 0; |
| 1537 | |
| 1538 | if (copy_from_user(&dr, arg, sizeof(dr))) |
| 1539 | return -EFAULT; |
| 1540 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1541 | hdev = hci_dev_get(dr.dev_id); |
| 1542 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | return -ENODEV; |
| 1544 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1545 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1546 | err = -EBUSY; |
| 1547 | goto done; |
| 1548 | } |
| 1549 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 1550 | if (hdev->dev_type != HCI_BREDR) { |
| 1551 | err = -EOPNOTSUPP; |
| 1552 | goto done; |
| 1553 | } |
| 1554 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1555 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1556 | err = -EOPNOTSUPP; |
| 1557 | goto done; |
| 1558 | } |
| 1559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | switch (cmd) { |
| 1561 | case HCISETAUTH: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1562 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
| 1563 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | break; |
| 1565 | |
| 1566 | case HCISETENCRYPT: |
| 1567 | if (!lmp_encrypt_capable(hdev)) { |
| 1568 | err = -EOPNOTSUPP; |
| 1569 | break; |
| 1570 | } |
| 1571 | |
| 1572 | if (!test_bit(HCI_AUTH, &hdev->flags)) { |
| 1573 | /* Auth must be enabled first */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1574 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
| 1575 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | if (err) |
| 1577 | break; |
| 1578 | } |
| 1579 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1580 | err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt, |
| 1581 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | break; |
| 1583 | |
| 1584 | case HCISETSCAN: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1585 | err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt, |
| 1586 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | break; |
| 1588 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1589 | case HCISETLINKPOL: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1590 | err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt, |
| 1591 | HCI_INIT_TIMEOUT); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1592 | break; |
| 1593 | |
| 1594 | case HCISETLINKMODE: |
| 1595 | hdev->link_mode = ((__u16) dr.dev_opt) & |
| 1596 | (HCI_LM_MASTER | HCI_LM_ACCEPT); |
| 1597 | break; |
| 1598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | case HCISETPTYPE: |
| 1600 | hdev->pkt_type = (__u16) dr.dev_opt; |
| 1601 | break; |
| 1602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | case HCISETACLMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1604 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 1605 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | break; |
| 1607 | |
| 1608 | case HCISETSCOMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1609 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 1610 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | break; |
| 1612 | |
| 1613 | default: |
| 1614 | err = -EINVAL; |
| 1615 | break; |
| 1616 | } |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1617 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1618 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | hci_dev_put(hdev); |
| 1620 | return err; |
| 1621 | } |
| 1622 | |
| 1623 | int hci_get_dev_list(void __user *arg) |
| 1624 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1625 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | struct hci_dev_list_req *dl; |
| 1627 | struct hci_dev_req *dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | int n = 0, size, err; |
| 1629 | __u16 dev_num; |
| 1630 | |
| 1631 | if (get_user(dev_num, (__u16 __user *) arg)) |
| 1632 | return -EFAULT; |
| 1633 | |
| 1634 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) |
| 1635 | return -EINVAL; |
| 1636 | |
| 1637 | size = sizeof(*dl) + dev_num * sizeof(*dr); |
| 1638 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1639 | dl = kzalloc(size, GFP_KERNEL); |
| 1640 | if (!dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | return -ENOMEM; |
| 1642 | |
| 1643 | dr = dl->dev_req; |
| 1644 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1645 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1646 | list_for_each_entry(hdev, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1647 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 1648 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1649 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1650 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 1651 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | (dr + n)->dev_id = hdev->id; |
| 1654 | (dr + n)->dev_opt = hdev->flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | if (++n >= dev_num) |
| 1657 | break; |
| 1658 | } |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1659 | read_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
| 1661 | dl->dev_num = n; |
| 1662 | size = sizeof(*dl) + n * sizeof(*dr); |
| 1663 | |
| 1664 | err = copy_to_user(arg, dl, size); |
| 1665 | kfree(dl); |
| 1666 | |
| 1667 | return err ? -EFAULT : 0; |
| 1668 | } |
| 1669 | |
| 1670 | int hci_get_dev_info(void __user *arg) |
| 1671 | { |
| 1672 | struct hci_dev *hdev; |
| 1673 | struct hci_dev_info di; |
| 1674 | int err = 0; |
| 1675 | |
| 1676 | if (copy_from_user(&di, arg, sizeof(di))) |
| 1677 | return -EFAULT; |
| 1678 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1679 | hdev = hci_dev_get(di.dev_id); |
| 1680 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | return -ENODEV; |
| 1682 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1683 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 1684 | cancel_delayed_work_sync(&hdev->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1685 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1686 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 1687 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | strcpy(di.name, hdev->name); |
| 1690 | di.bdaddr = hdev->bdaddr; |
Marcel Holtmann | 60f2a3e | 2013-10-01 22:59:20 -0700 | [diff] [blame] | 1691 | di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | di.flags = hdev->flags; |
| 1693 | di.pkt_type = hdev->pkt_type; |
Johan Hedberg | 572c7f8 | 2012-10-19 20:57:46 +0300 | [diff] [blame] | 1694 | if (lmp_bredr_capable(hdev)) { |
| 1695 | di.acl_mtu = hdev->acl_mtu; |
| 1696 | di.acl_pkts = hdev->acl_pkts; |
| 1697 | di.sco_mtu = hdev->sco_mtu; |
| 1698 | di.sco_pkts = hdev->sco_pkts; |
| 1699 | } else { |
| 1700 | di.acl_mtu = hdev->le_mtu; |
| 1701 | di.acl_pkts = hdev->le_pkts; |
| 1702 | di.sco_mtu = 0; |
| 1703 | di.sco_pkts = 0; |
| 1704 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | di.link_policy = hdev->link_policy; |
| 1706 | di.link_mode = hdev->link_mode; |
| 1707 | |
| 1708 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); |
| 1709 | memcpy(&di.features, &hdev->features, sizeof(di.features)); |
| 1710 | |
| 1711 | if (copy_to_user(arg, &di, sizeof(di))) |
| 1712 | err = -EFAULT; |
| 1713 | |
| 1714 | hci_dev_put(hdev); |
| 1715 | |
| 1716 | return err; |
| 1717 | } |
| 1718 | |
| 1719 | /* ---- Interface to HCI drivers ---- */ |
| 1720 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1721 | static int hci_rfkill_set_block(void *data, bool blocked) |
| 1722 | { |
| 1723 | struct hci_dev *hdev = data; |
| 1724 | |
| 1725 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); |
| 1726 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1727 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) |
| 1728 | return -EBUSY; |
| 1729 | |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 1730 | if (blocked) { |
| 1731 | set_bit(HCI_RFKILLED, &hdev->dev_flags); |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 1732 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 1733 | hci_dev_do_close(hdev); |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 1734 | } else { |
| 1735 | clear_bit(HCI_RFKILLED, &hdev->dev_flags); |
Gustavo Padovan | 1025c04 | 2013-09-27 11:56:14 -0300 | [diff] [blame] | 1736 | } |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1737 | |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | static const struct rfkill_ops hci_rfkill_ops = { |
| 1742 | .set_block = hci_rfkill_set_block, |
| 1743 | }; |
| 1744 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1745 | static void hci_power_on(struct work_struct *work) |
| 1746 | { |
| 1747 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 1748 | int err; |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1749 | |
| 1750 | BT_DBG("%s", hdev->name); |
| 1751 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1752 | err = hci_dev_do_open(hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 1753 | if (err < 0) { |
| 1754 | mgmt_set_powered_failed(hdev, err); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1755 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 1756 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1757 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1758 | /* During the HCI setup phase, a few error conditions are |
| 1759 | * ignored and they need to be checked now. If they are still |
| 1760 | * valid, it is important to turn the device back off. |
| 1761 | */ |
| 1762 | if (test_bit(HCI_RFKILLED, &hdev->dev_flags) || |
| 1763 | (hdev->dev_type == HCI_BREDR && |
| 1764 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && |
| 1765 | !bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 1766 | clear_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 1767 | hci_dev_do_close(hdev); |
| 1768 | } else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1769 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1770 | HCI_AUTO_OFF_TIMEOUT); |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 1771 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1772 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1773 | if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1774 | mgmt_index_added(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | static void hci_power_off(struct work_struct *work) |
| 1778 | { |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 1779 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1780 | power_off.work); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1781 | |
| 1782 | BT_DBG("%s", hdev->name); |
| 1783 | |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1784 | hci_dev_do_close(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1787 | static void hci_discov_off(struct work_struct *work) |
| 1788 | { |
| 1789 | struct hci_dev *hdev; |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1790 | |
| 1791 | hdev = container_of(work, struct hci_dev, discov_off.work); |
| 1792 | |
| 1793 | BT_DBG("%s", hdev->name); |
| 1794 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 1795 | mgmt_discoverable_timeout(hdev); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1796 | } |
| 1797 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1798 | int hci_uuids_clear(struct hci_dev *hdev) |
| 1799 | { |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 1800 | struct bt_uuid *uuid, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1801 | |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 1802 | list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) { |
| 1803 | list_del(&uuid->list); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1804 | kfree(uuid); |
| 1805 | } |
| 1806 | |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1810 | int hci_link_keys_clear(struct hci_dev *hdev) |
| 1811 | { |
| 1812 | struct list_head *p, *n; |
| 1813 | |
| 1814 | list_for_each_safe(p, n, &hdev->link_keys) { |
| 1815 | struct link_key *key; |
| 1816 | |
| 1817 | key = list_entry(p, struct link_key, list); |
| 1818 | |
| 1819 | list_del(p); |
| 1820 | kfree(key); |
| 1821 | } |
| 1822 | |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 1826 | int hci_smp_ltks_clear(struct hci_dev *hdev) |
| 1827 | { |
| 1828 | struct smp_ltk *k, *tmp; |
| 1829 | |
| 1830 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
| 1831 | list_del(&k->list); |
| 1832 | kfree(k); |
| 1833 | } |
| 1834 | |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1838 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1839 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1840 | struct link_key *k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1841 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1842 | list_for_each_entry(k, &hdev->link_keys, list) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1843 | if (bacmp(bdaddr, &k->bdaddr) == 0) |
| 1844 | return k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1845 | |
| 1846 | return NULL; |
| 1847 | } |
| 1848 | |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1849 | static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1850 | u8 key_type, u8 old_key_type) |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1851 | { |
| 1852 | /* Legacy key */ |
| 1853 | if (key_type < 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1854 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1855 | |
| 1856 | /* Debug keys are insecure so don't store them persistently */ |
| 1857 | if (key_type == HCI_LK_DEBUG_COMBINATION) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1858 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1859 | |
| 1860 | /* Changed combination key and there's no previous one */ |
| 1861 | if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1862 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1863 | |
| 1864 | /* Security mode 3 case */ |
| 1865 | if (!conn) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1866 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1867 | |
| 1868 | /* Neither local nor remote side had no-bonding as requirement */ |
| 1869 | if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1870 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1871 | |
| 1872 | /* Local side had dedicated bonding as requirement */ |
| 1873 | if (conn->auth_type == 0x02 || conn->auth_type == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1874 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1875 | |
| 1876 | /* Remote side had dedicated bonding as requirement */ |
| 1877 | if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1878 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1879 | |
| 1880 | /* If none of the above criteria match, then don't store the key |
| 1881 | * persistently */ |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1882 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1885 | struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1886 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1887 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1888 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1889 | list_for_each_entry(k, &hdev->long_term_keys, list) { |
| 1890 | if (k->ediv != ediv || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1891 | memcmp(rand, k->rand, sizeof(k->rand))) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1892 | continue; |
| 1893 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1894 | return k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | return NULL; |
| 1898 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1899 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1900 | struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1901 | u8 addr_type) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1902 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1903 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1904 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1905 | list_for_each_entry(k, &hdev->long_term_keys, list) |
| 1906 | if (addr_type == k->bdaddr_type && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1907 | bacmp(bdaddr, &k->bdaddr) == 0) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1908 | return k; |
| 1909 | |
| 1910 | return NULL; |
| 1911 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1912 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1913 | int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1914 | bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1915 | { |
| 1916 | struct link_key *key, *old_key; |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1917 | u8 old_key_type; |
| 1918 | bool persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1919 | |
| 1920 | old_key = hci_find_link_key(hdev, bdaddr); |
| 1921 | if (old_key) { |
| 1922 | old_key_type = old_key->type; |
| 1923 | key = old_key; |
| 1924 | } else { |
Johan Hedberg | 12adcf3 | 2011-04-28 11:29:00 -0700 | [diff] [blame] | 1925 | old_key_type = conn ? conn->key_type : 0xff; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1926 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
| 1927 | if (!key) |
| 1928 | return -ENOMEM; |
| 1929 | list_add(&key->list, &hdev->link_keys); |
| 1930 | } |
| 1931 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1932 | BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1933 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1934 | /* Some buggy controller combinations generate a changed |
| 1935 | * combination key for legacy pairing even when there's no |
| 1936 | * previous key */ |
| 1937 | if (type == HCI_LK_CHANGED_COMBINATION && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1938 | (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) { |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1939 | type = HCI_LK_COMBINATION; |
Johan Hedberg | 655fe6e | 2011-04-28 11:29:01 -0700 | [diff] [blame] | 1940 | if (conn) |
| 1941 | conn->key_type = type; |
| 1942 | } |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1943 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1944 | bacpy(&key->bdaddr, bdaddr); |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 1945 | memcpy(key->val, val, HCI_LINK_KEY_SIZE); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1946 | key->pin_len = pin_len; |
| 1947 | |
Waldemar Rymarkiewicz | b6020ba | 2011-04-28 12:07:53 +0200 | [diff] [blame] | 1948 | if (type == HCI_LK_CHANGED_COMBINATION) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1949 | key->type = old_key_type; |
Johan Hedberg | 4748fed | 2011-04-28 11:29:02 -0700 | [diff] [blame] | 1950 | else |
| 1951 | key->type = type; |
| 1952 | |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1953 | if (!new_key) |
| 1954 | return 0; |
| 1955 | |
| 1956 | persistent = hci_persistent_key(hdev, conn, type, old_key_type); |
| 1957 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1958 | mgmt_new_link_key(hdev, key, persistent); |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1959 | |
Vishal Agarwal | 6ec5bca | 2012-04-16 14:44:44 +0530 | [diff] [blame] | 1960 | if (conn) |
| 1961 | conn->flush_key = !persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1962 | |
| 1963 | return 0; |
| 1964 | } |
| 1965 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1966 | int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type, |
Andrei Emeltchenko | 9a00665 | 2012-03-09 12:12:12 +0200 | [diff] [blame] | 1967 | int new_key, u8 authenticated, u8 tk[16], u8 enc_size, __le16 |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1968 | ediv, u8 rand[8]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1969 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1970 | struct smp_ltk *key, *old_key; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1971 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1972 | if (!(type & HCI_SMP_STK) && !(type & HCI_SMP_LTK)) |
| 1973 | return 0; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1974 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1975 | old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type); |
| 1976 | if (old_key) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1977 | key = old_key; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1978 | else { |
| 1979 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1980 | if (!key) |
| 1981 | return -ENOMEM; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1982 | list_add(&key->list, &hdev->long_term_keys); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1983 | } |
| 1984 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1985 | bacpy(&key->bdaddr, bdaddr); |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1986 | key->bdaddr_type = addr_type; |
| 1987 | memcpy(key->val, tk, sizeof(key->val)); |
| 1988 | key->authenticated = authenticated; |
| 1989 | key->ediv = ediv; |
| 1990 | key->enc_size = enc_size; |
| 1991 | key->type = type; |
| 1992 | memcpy(key->rand, rand, sizeof(key->rand)); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1993 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1994 | if (!new_key) |
| 1995 | return 0; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1996 | |
Vinicius Costa Gomes | 261cc5a | 2012-02-02 21:08:05 -0300 | [diff] [blame] | 1997 | if (type & HCI_SMP_LTK) |
| 1998 | mgmt_new_ltk(hdev, key, 1); |
| 1999 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2000 | return 0; |
| 2001 | } |
| 2002 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2003 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2004 | { |
| 2005 | struct link_key *key; |
| 2006 | |
| 2007 | key = hci_find_link_key(hdev, bdaddr); |
| 2008 | if (!key) |
| 2009 | return -ENOENT; |
| 2010 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2011 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2012 | |
| 2013 | list_del(&key->list); |
| 2014 | kfree(key); |
| 2015 | |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2019 | int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2020 | { |
| 2021 | struct smp_ltk *k, *tmp; |
| 2022 | |
| 2023 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
| 2024 | if (bacmp(bdaddr, &k->bdaddr)) |
| 2025 | continue; |
| 2026 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2027 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2028 | |
| 2029 | list_del(&k->list); |
| 2030 | kfree(k); |
| 2031 | } |
| 2032 | |
| 2033 | return 0; |
| 2034 | } |
| 2035 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2036 | /* HCI command timer function */ |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2037 | static void hci_cmd_timeout(unsigned long arg) |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2038 | { |
| 2039 | struct hci_dev *hdev = (void *) arg; |
| 2040 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2041 | if (hdev->sent_cmd) { |
| 2042 | struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; |
| 2043 | u16 opcode = __le16_to_cpu(sent->opcode); |
| 2044 | |
| 2045 | BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode); |
| 2046 | } else { |
| 2047 | BT_ERR("%s command tx timeout", hdev->name); |
| 2048 | } |
| 2049 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2050 | atomic_set(&hdev->cmd_cnt, 1); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2051 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2054 | struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2055 | bdaddr_t *bdaddr) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2056 | { |
| 2057 | struct oob_data *data; |
| 2058 | |
| 2059 | list_for_each_entry(data, &hdev->remote_oob_data, list) |
| 2060 | if (bacmp(bdaddr, &data->bdaddr) == 0) |
| 2061 | return data; |
| 2062 | |
| 2063 | return NULL; |
| 2064 | } |
| 2065 | |
| 2066 | int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2067 | { |
| 2068 | struct oob_data *data; |
| 2069 | |
| 2070 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 2071 | if (!data) |
| 2072 | return -ENOENT; |
| 2073 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2074 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2075 | |
| 2076 | list_del(&data->list); |
| 2077 | kfree(data); |
| 2078 | |
| 2079 | return 0; |
| 2080 | } |
| 2081 | |
| 2082 | int hci_remote_oob_data_clear(struct hci_dev *hdev) |
| 2083 | { |
| 2084 | struct oob_data *data, *n; |
| 2085 | |
| 2086 | list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { |
| 2087 | list_del(&data->list); |
| 2088 | kfree(data); |
| 2089 | } |
| 2090 | |
| 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2095 | u8 *randomizer) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2096 | { |
| 2097 | struct oob_data *data; |
| 2098 | |
| 2099 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 2100 | |
| 2101 | if (!data) { |
| 2102 | data = kmalloc(sizeof(*data), GFP_ATOMIC); |
| 2103 | if (!data) |
| 2104 | return -ENOMEM; |
| 2105 | |
| 2106 | bacpy(&data->bdaddr, bdaddr); |
| 2107 | list_add(&data->list, &hdev->remote_oob_data); |
| 2108 | } |
| 2109 | |
| 2110 | memcpy(data->hash, hash, sizeof(data->hash)); |
| 2111 | memcpy(data->randomizer, randomizer, sizeof(data->randomizer)); |
| 2112 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2113 | BT_DBG("%s for %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2114 | |
| 2115 | return 0; |
| 2116 | } |
| 2117 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2118 | struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2119 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2120 | struct bdaddr_list *b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2121 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2122 | list_for_each_entry(b, &hdev->blacklist, list) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2123 | if (bacmp(bdaddr, &b->bdaddr) == 0) |
| 2124 | return b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2125 | |
| 2126 | return NULL; |
| 2127 | } |
| 2128 | |
| 2129 | int hci_blacklist_clear(struct hci_dev *hdev) |
| 2130 | { |
| 2131 | struct list_head *p, *n; |
| 2132 | |
| 2133 | list_for_each_safe(p, n, &hdev->blacklist) { |
| 2134 | struct bdaddr_list *b; |
| 2135 | |
| 2136 | b = list_entry(p, struct bdaddr_list, list); |
| 2137 | |
| 2138 | list_del(p); |
| 2139 | kfree(b); |
| 2140 | } |
| 2141 | |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2145 | int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2146 | { |
| 2147 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2148 | |
| 2149 | if (bacmp(bdaddr, BDADDR_ANY) == 0) |
| 2150 | return -EBADF; |
| 2151 | |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2152 | if (hci_blacklist_lookup(hdev, bdaddr)) |
| 2153 | return -EEXIST; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2154 | |
| 2155 | entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2156 | if (!entry) |
| 2157 | return -ENOMEM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2158 | |
| 2159 | bacpy(&entry->bdaddr, bdaddr); |
| 2160 | |
| 2161 | list_add(&entry->list, &hdev->blacklist); |
| 2162 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2163 | return mgmt_device_blocked(hdev, bdaddr, type); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2164 | } |
| 2165 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2166 | int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2167 | { |
| 2168 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2169 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 2170 | if (bacmp(bdaddr, BDADDR_ANY) == 0) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2171 | return hci_blacklist_clear(hdev); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2172 | |
| 2173 | entry = hci_blacklist_lookup(hdev, bdaddr); |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 2174 | if (!entry) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2175 | return -ENOENT; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2176 | |
| 2177 | list_del(&entry->list); |
| 2178 | kfree(entry); |
| 2179 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2180 | return mgmt_device_unblocked(hdev, bdaddr, type); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2181 | } |
| 2182 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2183 | static void inquiry_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2184 | { |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2185 | if (status) { |
| 2186 | BT_ERR("Failed to start inquiry: status %d", status); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2187 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2188 | hci_dev_lock(hdev); |
| 2189 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2190 | hci_dev_unlock(hdev); |
| 2191 | return; |
| 2192 | } |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2193 | } |
| 2194 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2195 | static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2196 | { |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2197 | /* General inquiry access code (GIAC) */ |
| 2198 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 2199 | struct hci_request req; |
| 2200 | struct hci_cp_inquiry cp; |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2201 | int err; |
| 2202 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2203 | if (status) { |
| 2204 | BT_ERR("Failed to disable LE scanning: status %d", status); |
| 2205 | return; |
Andre Guedes | 7dbfac1 | 2012-03-15 16:52:07 -0300 | [diff] [blame] | 2206 | } |
| 2207 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2208 | switch (hdev->discovery.type) { |
| 2209 | case DISCOV_TYPE_LE: |
| 2210 | hci_dev_lock(hdev); |
| 2211 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2212 | hci_dev_unlock(hdev); |
| 2213 | break; |
| 2214 | |
| 2215 | case DISCOV_TYPE_INTERLEAVED: |
| 2216 | hci_req_init(&req, hdev); |
| 2217 | |
| 2218 | memset(&cp, 0, sizeof(cp)); |
| 2219 | memcpy(&cp.lap, lap, sizeof(cp.lap)); |
| 2220 | cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN; |
| 2221 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 2222 | |
| 2223 | hci_dev_lock(hdev); |
| 2224 | |
| 2225 | hci_inquiry_cache_flush(hdev); |
| 2226 | |
| 2227 | err = hci_req_run(&req, inquiry_complete); |
| 2228 | if (err) { |
| 2229 | BT_ERR("Inquiry request failed: err %d", err); |
| 2230 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2231 | } |
| 2232 | |
| 2233 | hci_dev_unlock(hdev); |
| 2234 | break; |
| 2235 | } |
Andre Guedes | 7dbfac1 | 2012-03-15 16:52:07 -0300 | [diff] [blame] | 2236 | } |
| 2237 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2238 | static void le_scan_disable_work(struct work_struct *work) |
| 2239 | { |
| 2240 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2241 | le_scan_disable.work); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2242 | struct hci_cp_le_set_scan_enable cp; |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2243 | struct hci_request req; |
| 2244 | int err; |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2245 | |
| 2246 | BT_DBG("%s", hdev->name); |
| 2247 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2248 | hci_req_init(&req, hdev); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2249 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2250 | memset(&cp, 0, sizeof(cp)); |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2251 | cp.enable = LE_SCAN_DISABLE; |
| 2252 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2253 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 2254 | err = hci_req_run(&req, le_scan_disable_work_complete); |
| 2255 | if (err) |
| 2256 | BT_ERR("Disable LE scanning request failed: err %d", err); |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 2257 | } |
| 2258 | |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2259 | /* Alloc HCI device */ |
| 2260 | struct hci_dev *hci_alloc_dev(void) |
| 2261 | { |
| 2262 | struct hci_dev *hdev; |
| 2263 | |
| 2264 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); |
| 2265 | if (!hdev) |
| 2266 | return NULL; |
| 2267 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2268 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); |
| 2269 | hdev->esco_type = (ESCO_HV1); |
| 2270 | hdev->link_mode = (HCI_LM_ACCEPT); |
Marcel Holtmann | b4cb9fb | 2013-10-14 13:56:16 -0700 | [diff] [blame] | 2271 | hdev->num_iac = 0x01; /* One IAC support is mandatory */ |
| 2272 | hdev->io_capability = 0x03; /* No Input No Output */ |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 2273 | hdev->inq_tx_power = HCI_TX_POWER_INVALID; |
| 2274 | hdev->adv_tx_power = HCI_TX_POWER_INVALID; |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2275 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2276 | hdev->sniff_max_interval = 800; |
| 2277 | hdev->sniff_min_interval = 80; |
| 2278 | |
Marcel Holtmann | bef6473 | 2013-10-11 08:23:19 -0700 | [diff] [blame] | 2279 | hdev->le_scan_interval = 0x0060; |
| 2280 | hdev->le_scan_window = 0x0030; |
| 2281 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2282 | mutex_init(&hdev->lock); |
| 2283 | mutex_init(&hdev->req_lock); |
| 2284 | |
| 2285 | INIT_LIST_HEAD(&hdev->mgmt_pending); |
| 2286 | INIT_LIST_HEAD(&hdev->blacklist); |
| 2287 | INIT_LIST_HEAD(&hdev->uuids); |
| 2288 | INIT_LIST_HEAD(&hdev->link_keys); |
| 2289 | INIT_LIST_HEAD(&hdev->long_term_keys); |
| 2290 | INIT_LIST_HEAD(&hdev->remote_oob_data); |
Andrei Emeltchenko | 6b536b5 | 2012-08-31 16:39:28 +0300 | [diff] [blame] | 2291 | INIT_LIST_HEAD(&hdev->conn_hash.list); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2292 | |
| 2293 | INIT_WORK(&hdev->rx_work, hci_rx_work); |
| 2294 | INIT_WORK(&hdev->cmd_work, hci_cmd_work); |
| 2295 | INIT_WORK(&hdev->tx_work, hci_tx_work); |
| 2296 | INIT_WORK(&hdev->power_on, hci_power_on); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2297 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2298 | INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); |
| 2299 | INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); |
| 2300 | INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); |
| 2301 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2302 | skb_queue_head_init(&hdev->rx_q); |
| 2303 | skb_queue_head_init(&hdev->cmd_q); |
| 2304 | skb_queue_head_init(&hdev->raw_q); |
| 2305 | |
| 2306 | init_waitqueue_head(&hdev->req_wait_q); |
| 2307 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2308 | setup_timer(&hdev->cmd_timer, hci_cmd_timeout, (unsigned long) hdev); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2309 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2310 | hci_init_sysfs(hdev); |
| 2311 | discovery_init(hdev); |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2312 | |
| 2313 | return hdev; |
| 2314 | } |
| 2315 | EXPORT_SYMBOL(hci_alloc_dev); |
| 2316 | |
| 2317 | /* Free HCI device */ |
| 2318 | void hci_free_dev(struct hci_dev *hdev) |
| 2319 | { |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2320 | /* will free via device release */ |
| 2321 | put_device(&hdev->dev); |
| 2322 | } |
| 2323 | EXPORT_SYMBOL(hci_free_dev); |
| 2324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | /* Register HCI device */ |
| 2326 | int hci_register_dev(struct hci_dev *hdev) |
| 2327 | { |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2328 | int id, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | |
David Herrmann | 010666a | 2012-01-07 15:47:07 +0100 | [diff] [blame] | 2330 | if (!hdev->open || !hdev->close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | return -EINVAL; |
| 2332 | |
Mat Martineau | 08add51 | 2011-11-02 16:18:36 -0700 | [diff] [blame] | 2333 | /* Do not allow HCI_AMP devices to register at index 0, |
| 2334 | * so the index can be used as the AMP controller ID. |
| 2335 | */ |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2336 | switch (hdev->dev_type) { |
| 2337 | case HCI_BREDR: |
| 2338 | id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL); |
| 2339 | break; |
| 2340 | case HCI_AMP: |
| 2341 | id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL); |
| 2342 | break; |
| 2343 | default: |
| 2344 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2346 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2347 | if (id < 0) |
| 2348 | return id; |
| 2349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | sprintf(hdev->name, "hci%d", id); |
| 2351 | hdev->id = id; |
Andrei Emeltchenko | 2d8b3a1 | 2012-04-16 16:32:04 +0300 | [diff] [blame] | 2352 | |
| 2353 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
| 2354 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 2355 | hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | |
| 2356 | WQ_MEM_RECLAIM, 1, hdev->name); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2357 | if (!hdev->workqueue) { |
| 2358 | error = -ENOMEM; |
| 2359 | goto err; |
| 2360 | } |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2361 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 2362 | hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | |
| 2363 | WQ_MEM_RECLAIM, 1, hdev->name); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2364 | if (!hdev->req_workqueue) { |
| 2365 | destroy_workqueue(hdev->workqueue); |
| 2366 | error = -ENOMEM; |
| 2367 | goto err; |
| 2368 | } |
| 2369 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2370 | error = hci_add_sysfs(hdev); |
| 2371 | if (error < 0) |
| 2372 | goto err_wqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2374 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2375 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, |
| 2376 | hdev); |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2377 | if (hdev->rfkill) { |
| 2378 | if (rfkill_register(hdev->rfkill) < 0) { |
| 2379 | rfkill_destroy(hdev->rfkill); |
| 2380 | hdev->rfkill = NULL; |
| 2381 | } |
| 2382 | } |
| 2383 | |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 2384 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) |
| 2385 | set_bit(HCI_RFKILLED, &hdev->dev_flags); |
| 2386 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2387 | set_bit(HCI_SETUP, &hdev->dev_flags); |
Marcel Holtmann | 004b025 | 2013-10-07 00:58:32 -0700 | [diff] [blame] | 2388 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 2389 | |
Marcel Holtmann | 01cd340 | 2013-10-06 01:16:22 -0700 | [diff] [blame] | 2390 | if (hdev->dev_type == HCI_BREDR) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2391 | /* Assume BR/EDR support until proven otherwise (such as |
| 2392 | * through reading supported features during init. |
| 2393 | */ |
| 2394 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 2395 | } |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 2396 | |
Gustavo Padovan | fcee337 | 2013-07-11 11:34:28 +0100 | [diff] [blame] | 2397 | write_lock(&hci_dev_list_lock); |
| 2398 | list_add(&hdev->list, &hci_dev_list); |
| 2399 | write_unlock(&hci_dev_list_lock); |
| 2400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | hci_notify(hdev, HCI_DEV_REG); |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 2402 | hci_dev_hold(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 2404 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Marcel Holtmann | fbe96d6 | 2012-10-30 01:35:40 -0700 | [diff] [blame] | 2405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | return id; |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2407 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2408 | err_wqueue: |
| 2409 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2410 | destroy_workqueue(hdev->req_workqueue); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2411 | err: |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2412 | ida_simple_remove(&hci_index_ida, hdev->id); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2413 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2414 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | } |
| 2416 | EXPORT_SYMBOL(hci_register_dev); |
| 2417 | |
| 2418 | /* Unregister HCI device */ |
David Herrmann | 5973563 | 2011-10-26 10:43:19 +0200 | [diff] [blame] | 2419 | void hci_unregister_dev(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | { |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2421 | int i, id; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2422 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 2423 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 2425 | set_bit(HCI_UNREGISTER, &hdev->dev_flags); |
| 2426 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2427 | id = hdev->id; |
| 2428 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2429 | write_lock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | list_del(&hdev->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2431 | write_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | |
| 2433 | hci_dev_do_close(hdev); |
| 2434 | |
Suraj Sumangala | cd4c539 | 2010-07-14 13:02:16 +0530 | [diff] [blame] | 2435 | for (i = 0; i < NUM_REASSEMBLY; i++) |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2436 | kfree_skb(hdev->reassembly[i]); |
| 2437 | |
Gustavo Padovan | b9b5ef1 | 2012-11-21 00:50:21 -0200 | [diff] [blame] | 2438 | cancel_work_sync(&hdev->power_on); |
| 2439 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2440 | if (!test_bit(HCI_INIT, &hdev->flags) && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2441 | !test_bit(HCI_SETUP, &hdev->dev_flags)) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2442 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2443 | mgmt_index_removed(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2444 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2445 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2446 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2447 | /* mgmt_index_removed should take care of emptying the |
| 2448 | * pending list */ |
| 2449 | BUG_ON(!list_empty(&hdev->mgmt_pending)); |
| 2450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | hci_notify(hdev, HCI_DEV_UNREG); |
| 2452 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2453 | if (hdev->rfkill) { |
| 2454 | rfkill_unregister(hdev->rfkill); |
| 2455 | rfkill_destroy(hdev->rfkill); |
| 2456 | } |
| 2457 | |
David Herrmann | ce24297 | 2011-10-08 14:58:48 +0200 | [diff] [blame] | 2458 | hci_del_sysfs(hdev); |
Dave Young | 147e2d5 | 2008-03-05 18:45:59 -0800 | [diff] [blame] | 2459 | |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2460 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2461 | destroy_workqueue(hdev->req_workqueue); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2462 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2463 | hci_dev_lock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 2464 | hci_blacklist_clear(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2465 | hci_uuids_clear(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2466 | hci_link_keys_clear(hdev); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2467 | hci_smp_ltks_clear(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2468 | hci_remote_oob_data_clear(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2469 | hci_dev_unlock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 2470 | |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 2471 | hci_dev_put(hdev); |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2472 | |
| 2473 | ida_simple_remove(&hci_index_ida, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | } |
| 2475 | EXPORT_SYMBOL(hci_unregister_dev); |
| 2476 | |
| 2477 | /* Suspend HCI device */ |
| 2478 | int hci_suspend_dev(struct hci_dev *hdev) |
| 2479 | { |
| 2480 | hci_notify(hdev, HCI_DEV_SUSPEND); |
| 2481 | return 0; |
| 2482 | } |
| 2483 | EXPORT_SYMBOL(hci_suspend_dev); |
| 2484 | |
| 2485 | /* Resume HCI device */ |
| 2486 | int hci_resume_dev(struct hci_dev *hdev) |
| 2487 | { |
| 2488 | hci_notify(hdev, HCI_DEV_RESUME); |
| 2489 | return 0; |
| 2490 | } |
| 2491 | EXPORT_SYMBOL(hci_resume_dev); |
| 2492 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2493 | /* Receive frame from HCI drivers */ |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 2494 | int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb) |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2495 | { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2496 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2497 | && !test_bit(HCI_INIT, &hdev->flags))) { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2498 | kfree_skb(skb); |
| 2499 | return -ENXIO; |
| 2500 | } |
| 2501 | |
Jorrit Schippers | d82603c | 2012-12-27 17:33:02 +0100 | [diff] [blame] | 2502 | /* Incoming skb */ |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2503 | bt_cb(skb)->incoming = 1; |
| 2504 | |
| 2505 | /* Time stamp */ |
| 2506 | __net_timestamp(skb); |
| 2507 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2508 | skb_queue_tail(&hdev->rx_q, skb); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 2509 | queue_work(hdev->workqueue, &hdev->rx_work); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2510 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2511 | return 0; |
| 2512 | } |
| 2513 | EXPORT_SYMBOL(hci_recv_frame); |
| 2514 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2515 | static int hci_reassembly(struct hci_dev *hdev, int type, void *data, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2516 | int count, __u8 index) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2517 | { |
| 2518 | int len = 0; |
| 2519 | int hlen = 0; |
| 2520 | int remain = count; |
| 2521 | struct sk_buff *skb; |
| 2522 | struct bt_skb_cb *scb; |
| 2523 | |
| 2524 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2525 | index >= NUM_REASSEMBLY) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2526 | return -EILSEQ; |
| 2527 | |
| 2528 | skb = hdev->reassembly[index]; |
| 2529 | |
| 2530 | if (!skb) { |
| 2531 | switch (type) { |
| 2532 | case HCI_ACLDATA_PKT: |
| 2533 | len = HCI_MAX_FRAME_SIZE; |
| 2534 | hlen = HCI_ACL_HDR_SIZE; |
| 2535 | break; |
| 2536 | case HCI_EVENT_PKT: |
| 2537 | len = HCI_MAX_EVENT_SIZE; |
| 2538 | hlen = HCI_EVENT_HDR_SIZE; |
| 2539 | break; |
| 2540 | case HCI_SCODATA_PKT: |
| 2541 | len = HCI_MAX_SCO_SIZE; |
| 2542 | hlen = HCI_SCO_HDR_SIZE; |
| 2543 | break; |
| 2544 | } |
| 2545 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2546 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2547 | if (!skb) |
| 2548 | return -ENOMEM; |
| 2549 | |
| 2550 | scb = (void *) skb->cb; |
| 2551 | scb->expect = hlen; |
| 2552 | scb->pkt_type = type; |
| 2553 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2554 | hdev->reassembly[index] = skb; |
| 2555 | } |
| 2556 | |
| 2557 | while (count) { |
| 2558 | scb = (void *) skb->cb; |
Dan Carpenter | 89bb46d | 2012-02-28 09:57:59 +0300 | [diff] [blame] | 2559 | len = min_t(uint, scb->expect, count); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2560 | |
| 2561 | memcpy(skb_put(skb, len), data, len); |
| 2562 | |
| 2563 | count -= len; |
| 2564 | data += len; |
| 2565 | scb->expect -= len; |
| 2566 | remain = count; |
| 2567 | |
| 2568 | switch (type) { |
| 2569 | case HCI_EVENT_PKT: |
| 2570 | if (skb->len == HCI_EVENT_HDR_SIZE) { |
| 2571 | struct hci_event_hdr *h = hci_event_hdr(skb); |
| 2572 | scb->expect = h->plen; |
| 2573 | |
| 2574 | if (skb_tailroom(skb) < scb->expect) { |
| 2575 | kfree_skb(skb); |
| 2576 | hdev->reassembly[index] = NULL; |
| 2577 | return -ENOMEM; |
| 2578 | } |
| 2579 | } |
| 2580 | break; |
| 2581 | |
| 2582 | case HCI_ACLDATA_PKT: |
| 2583 | if (skb->len == HCI_ACL_HDR_SIZE) { |
| 2584 | struct hci_acl_hdr *h = hci_acl_hdr(skb); |
| 2585 | scb->expect = __le16_to_cpu(h->dlen); |
| 2586 | |
| 2587 | if (skb_tailroom(skb) < scb->expect) { |
| 2588 | kfree_skb(skb); |
| 2589 | hdev->reassembly[index] = NULL; |
| 2590 | return -ENOMEM; |
| 2591 | } |
| 2592 | } |
| 2593 | break; |
| 2594 | |
| 2595 | case HCI_SCODATA_PKT: |
| 2596 | if (skb->len == HCI_SCO_HDR_SIZE) { |
| 2597 | struct hci_sco_hdr *h = hci_sco_hdr(skb); |
| 2598 | scb->expect = h->dlen; |
| 2599 | |
| 2600 | if (skb_tailroom(skb) < scb->expect) { |
| 2601 | kfree_skb(skb); |
| 2602 | hdev->reassembly[index] = NULL; |
| 2603 | return -ENOMEM; |
| 2604 | } |
| 2605 | } |
| 2606 | break; |
| 2607 | } |
| 2608 | |
| 2609 | if (scb->expect == 0) { |
| 2610 | /* Complete frame */ |
| 2611 | |
| 2612 | bt_cb(skb)->pkt_type = type; |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 2613 | hci_recv_frame(hdev, skb); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2614 | |
| 2615 | hdev->reassembly[index] = NULL; |
| 2616 | return remain; |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | return remain; |
| 2621 | } |
| 2622 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2623 | int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) |
| 2624 | { |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2625 | int rem = 0; |
| 2626 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2627 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) |
| 2628 | return -EILSEQ; |
| 2629 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 2630 | while (count) { |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2631 | rem = hci_reassembly(hdev, type, data, count, type - 1); |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2632 | if (rem < 0) |
| 2633 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2634 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2635 | data += (count - rem); |
| 2636 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 2637 | } |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2638 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2639 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2640 | } |
| 2641 | EXPORT_SYMBOL(hci_recv_fragment); |
| 2642 | |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2643 | #define STREAM_REASSEMBLY 0 |
| 2644 | |
| 2645 | int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) |
| 2646 | { |
| 2647 | int type; |
| 2648 | int rem = 0; |
| 2649 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 2650 | while (count) { |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2651 | struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; |
| 2652 | |
| 2653 | if (!skb) { |
| 2654 | struct { char type; } *pkt; |
| 2655 | |
| 2656 | /* Start of the frame */ |
| 2657 | pkt = data; |
| 2658 | type = pkt->type; |
| 2659 | |
| 2660 | data++; |
| 2661 | count--; |
| 2662 | } else |
| 2663 | type = bt_cb(skb)->pkt_type; |
| 2664 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2665 | rem = hci_reassembly(hdev, type, data, count, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2666 | STREAM_REASSEMBLY); |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2667 | if (rem < 0) |
| 2668 | return rem; |
| 2669 | |
| 2670 | data += (count - rem); |
| 2671 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 2672 | } |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2673 | |
| 2674 | return rem; |
| 2675 | } |
| 2676 | EXPORT_SYMBOL(hci_recv_stream_fragment); |
| 2677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | /* ---- Interface to upper protocols ---- */ |
| 2679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | int hci_register_cb(struct hci_cb *cb) |
| 2681 | { |
| 2682 | BT_DBG("%p name %s", cb, cb->name); |
| 2683 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2684 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | list_add(&cb->list, &hci_cb_list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2686 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | |
| 2688 | return 0; |
| 2689 | } |
| 2690 | EXPORT_SYMBOL(hci_register_cb); |
| 2691 | |
| 2692 | int hci_unregister_cb(struct hci_cb *cb) |
| 2693 | { |
| 2694 | BT_DBG("%p name %s", cb, cb->name); |
| 2695 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2696 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | list_del(&cb->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2698 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | |
| 2700 | return 0; |
| 2701 | } |
| 2702 | EXPORT_SYMBOL(hci_unregister_cb); |
| 2703 | |
Marcel Holtmann | 5108699 | 2013-10-10 14:54:19 -0700 | [diff] [blame] | 2704 | static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | { |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2706 | 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] | 2707 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 2708 | /* Time stamp */ |
| 2709 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 2711 | /* Send copy to monitor */ |
| 2712 | hci_send_to_monitor(hdev, skb); |
| 2713 | |
| 2714 | if (atomic_read(&hdev->promisc)) { |
| 2715 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 2716 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 2720 | skb_orphan(skb); |
| 2721 | |
Marcel Holtmann | 7bd8f09 | 2013-10-11 06:19:18 -0700 | [diff] [blame] | 2722 | if (hdev->send(hdev, skb) < 0) |
Marcel Holtmann | 5108699 | 2013-10-10 14:54:19 -0700 | [diff] [blame] | 2723 | BT_ERR("%s sending frame failed", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2726 | void hci_req_init(struct hci_request *req, struct hci_dev *hdev) |
| 2727 | { |
| 2728 | skb_queue_head_init(&req->cmd_q); |
| 2729 | req->hdev = hdev; |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 2730 | req->err = 0; |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | int hci_req_run(struct hci_request *req, hci_req_complete_t complete) |
| 2734 | { |
| 2735 | struct hci_dev *hdev = req->hdev; |
| 2736 | struct sk_buff *skb; |
| 2737 | unsigned long flags; |
| 2738 | |
| 2739 | BT_DBG("length %u", skb_queue_len(&req->cmd_q)); |
| 2740 | |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 2741 | /* If an error occured during request building, remove all HCI |
| 2742 | * commands queued on the HCI request queue. |
| 2743 | */ |
| 2744 | if (req->err) { |
| 2745 | skb_queue_purge(&req->cmd_q); |
| 2746 | return req->err; |
| 2747 | } |
| 2748 | |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2749 | /* Do not allow empty requests */ |
| 2750 | if (skb_queue_empty(&req->cmd_q)) |
Andre Guedes | 382b0c3 | 2013-03-08 11:20:14 -0300 | [diff] [blame] | 2751 | return -ENODATA; |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2752 | |
| 2753 | skb = skb_peek_tail(&req->cmd_q); |
| 2754 | bt_cb(skb)->req.complete = complete; |
| 2755 | |
| 2756 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 2757 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); |
| 2758 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 2759 | |
| 2760 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 2761 | |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2765 | static struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 2766 | u32 plen, const void *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | { |
| 2768 | int len = HCI_COMMAND_HDR_SIZE + plen; |
| 2769 | struct hci_command_hdr *hdr; |
| 2770 | struct sk_buff *skb; |
| 2771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2773 | if (!skb) |
| 2774 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | |
| 2776 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2777 | hdr->opcode = cpu_to_le16(opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | hdr->plen = plen; |
| 2779 | |
| 2780 | if (plen) |
| 2781 | memcpy(skb_put(skb, plen), param, plen); |
| 2782 | |
| 2783 | BT_DBG("skb len %d", skb->len); |
| 2784 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2785 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2786 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2787 | return skb; |
| 2788 | } |
| 2789 | |
| 2790 | /* Send HCI command */ |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 2791 | int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, |
| 2792 | const void *param) |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2793 | { |
| 2794 | struct sk_buff *skb; |
| 2795 | |
| 2796 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 2797 | |
| 2798 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 2799 | if (!skb) { |
| 2800 | BT_ERR("%s no memory for command", hdev->name); |
| 2801 | return -ENOMEM; |
| 2802 | } |
| 2803 | |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 2804 | /* Stand-alone HCI commands must be flaged as |
| 2805 | * single-command requests. |
| 2806 | */ |
| 2807 | bt_cb(skb)->req.start = true; |
| 2808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2810 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | |
| 2812 | return 0; |
| 2813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2815 | /* Queue a command to an asynchronous HCI request */ |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 2816 | void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, |
| 2817 | const void *param, u8 event) |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2818 | { |
| 2819 | struct hci_dev *hdev = req->hdev; |
| 2820 | struct sk_buff *skb; |
| 2821 | |
| 2822 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 2823 | |
Andre Guedes | 34739c1 | 2013-03-08 11:20:18 -0300 | [diff] [blame] | 2824 | /* If an error occured during request building, there is no point in |
| 2825 | * queueing the HCI command. We can simply return. |
| 2826 | */ |
| 2827 | if (req->err) |
| 2828 | return; |
| 2829 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2830 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 2831 | if (!skb) { |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 2832 | BT_ERR("%s no memory for command (opcode 0x%4.4x)", |
| 2833 | hdev->name, opcode); |
| 2834 | req->err = -ENOMEM; |
Andre Guedes | e348fe6 | 2013-03-08 11:20:17 -0300 | [diff] [blame] | 2835 | return; |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | if (skb_queue_empty(&req->cmd_q)) |
| 2839 | bt_cb(skb)->req.start = true; |
| 2840 | |
Johan Hedberg | 02350a7 | 2013-04-03 21:50:29 +0300 | [diff] [blame] | 2841 | bt_cb(skb)->req.event = event; |
| 2842 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2843 | skb_queue_tail(&req->cmd_q, skb); |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2844 | } |
| 2845 | |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 2846 | void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, |
| 2847 | const void *param) |
Johan Hedberg | 02350a7 | 2013-04-03 21:50:29 +0300 | [diff] [blame] | 2848 | { |
| 2849 | hci_req_add_ev(req, opcode, plen, param, 0); |
| 2850 | } |
| 2851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | /* Get data from the previously sent command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2853 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | { |
| 2855 | struct hci_command_hdr *hdr; |
| 2856 | |
| 2857 | if (!hdev->sent_cmd) |
| 2858 | return NULL; |
| 2859 | |
| 2860 | hdr = (void *) hdev->sent_cmd->data; |
| 2861 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2862 | if (hdr->opcode != cpu_to_le16(opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | return NULL; |
| 2864 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 2865 | BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | |
| 2867 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; |
| 2868 | } |
| 2869 | |
| 2870 | /* Send ACL data */ |
| 2871 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) |
| 2872 | { |
| 2873 | struct hci_acl_hdr *hdr; |
| 2874 | int len = skb->len; |
| 2875 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 2876 | skb_push(skb, HCI_ACL_HDR_SIZE); |
| 2877 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2878 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2879 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
| 2880 | hdr->dlen = cpu_to_le16(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | } |
| 2882 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 2883 | static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2884 | struct sk_buff *skb, __u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 2886 | struct hci_conn *conn = chan->conn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | struct hci_dev *hdev = conn->hdev; |
| 2888 | struct sk_buff *list; |
| 2889 | |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 2890 | skb->len = skb_headlen(skb); |
| 2891 | skb->data_len = 0; |
| 2892 | |
| 2893 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | 204a6e5 | 2012-10-15 11:58:39 +0300 | [diff] [blame] | 2894 | |
| 2895 | switch (hdev->dev_type) { |
| 2896 | case HCI_BREDR: |
| 2897 | hci_add_acl_hdr(skb, conn->handle, flags); |
| 2898 | break; |
| 2899 | case HCI_AMP: |
| 2900 | hci_add_acl_hdr(skb, chan->handle, flags); |
| 2901 | break; |
| 2902 | default: |
| 2903 | BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type); |
| 2904 | return; |
| 2905 | } |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 2906 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2907 | list = skb_shinfo(skb)->frag_list; |
| 2908 | if (!list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | /* Non fragmented */ |
| 2910 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); |
| 2911 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2912 | skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | } else { |
| 2914 | /* Fragmented */ |
| 2915 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 2916 | |
| 2917 | skb_shinfo(skb)->frag_list = NULL; |
| 2918 | |
| 2919 | /* Queue all fragments atomically */ |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 2920 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2922 | __skb_queue_tail(queue, skb); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 2923 | |
| 2924 | flags &= ~ACL_START; |
| 2925 | flags |= ACL_CONT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | do { |
| 2927 | skb = list; list = list->next; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2928 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2929 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 2930 | hci_add_acl_hdr(skb, conn->handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | |
| 2932 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 2933 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2934 | __skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | } while (list); |
| 2936 | |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 2937 | spin_unlock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) |
| 2942 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 2943 | struct hci_dev *hdev = chan->conn->hdev; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2944 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 2945 | BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2946 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 2947 | hci_queue_acl(chan, &chan->data_q, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2949 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | |
| 2952 | /* Send SCO data */ |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 2953 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | { |
| 2955 | struct hci_dev *hdev = conn->hdev; |
| 2956 | struct hci_sco_hdr hdr; |
| 2957 | |
| 2958 | BT_DBG("%s len %d", hdev->name, skb->len); |
| 2959 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2960 | hdr.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | hdr.dlen = skb->len; |
| 2962 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 2963 | skb_push(skb, HCI_SCO_HDR_SIZE); |
| 2964 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2965 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2967 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | skb_queue_tail(&conn->data_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2970 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | |
| 2973 | /* ---- HCI TX task (outgoing data) ---- */ |
| 2974 | |
| 2975 | /* HCI Connection scheduler */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2976 | static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, |
| 2977 | int *quote) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | { |
| 2979 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2980 | struct hci_conn *conn = NULL, *c; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 2981 | unsigned int num = 0, min = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2983 | /* We don't have to lock device here. Connections are always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | * added and removed with TX task disabled. */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2985 | |
| 2986 | rcu_read_lock(); |
| 2987 | |
| 2988 | list_for_each_entry_rcu(c, &h->list, list) { |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 2989 | if (c->type != type || skb_queue_empty(&c->data_q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | continue; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 2991 | |
| 2992 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) |
| 2993 | continue; |
| 2994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | num++; |
| 2996 | |
| 2997 | if (c->sent < min) { |
| 2998 | min = c->sent; |
| 2999 | conn = c; |
| 3000 | } |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3001 | |
| 3002 | if (hci_conn_num(hdev, type) == num) |
| 3003 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | } |
| 3005 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3006 | rcu_read_unlock(); |
| 3007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | if (conn) { |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3009 | int cnt, q; |
| 3010 | |
| 3011 | switch (conn->type) { |
| 3012 | case ACL_LINK: |
| 3013 | cnt = hdev->acl_cnt; |
| 3014 | break; |
| 3015 | case SCO_LINK: |
| 3016 | case ESCO_LINK: |
| 3017 | cnt = hdev->sco_cnt; |
| 3018 | break; |
| 3019 | case LE_LINK: |
| 3020 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 3021 | break; |
| 3022 | default: |
| 3023 | cnt = 0; |
| 3024 | BT_ERR("Unknown link type"); |
| 3025 | } |
| 3026 | |
| 3027 | q = cnt / num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | *quote = q ? q : 1; |
| 3029 | } else |
| 3030 | *quote = 0; |
| 3031 | |
| 3032 | BT_DBG("conn %p quote %d", conn, *quote); |
| 3033 | return conn; |
| 3034 | } |
| 3035 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3036 | static void hci_link_tx_to(struct hci_dev *hdev, __u8 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | { |
| 3038 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3039 | struct hci_conn *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3041 | BT_ERR("%s link tx timeout", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3043 | rcu_read_lock(); |
| 3044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | /* Kill stalled connections */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3046 | list_for_each_entry_rcu(c, &h->list, list) { |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3047 | if (c->type == type && c->sent) { |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 3048 | BT_ERR("%s killing stalled connection %pMR", |
| 3049 | hdev->name, &c->dst); |
Andre Guedes | bed7174 | 2013-01-30 11:50:56 -0300 | [diff] [blame] | 3050 | hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | } |
| 3052 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3053 | |
| 3054 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | } |
| 3056 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3057 | static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, |
| 3058 | int *quote) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3059 | { |
| 3060 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 3061 | struct hci_chan *chan = NULL; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 3062 | unsigned int num = 0, min = ~0, cur_prio = 0; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3063 | struct hci_conn *conn; |
| 3064 | int cnt, q, conn_num = 0; |
| 3065 | |
| 3066 | BT_DBG("%s", hdev->name); |
| 3067 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3068 | rcu_read_lock(); |
| 3069 | |
| 3070 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3071 | struct hci_chan *tmp; |
| 3072 | |
| 3073 | if (conn->type != type) |
| 3074 | continue; |
| 3075 | |
| 3076 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 3077 | continue; |
| 3078 | |
| 3079 | conn_num++; |
| 3080 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 3081 | list_for_each_entry_rcu(tmp, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3082 | struct sk_buff *skb; |
| 3083 | |
| 3084 | if (skb_queue_empty(&tmp->data_q)) |
| 3085 | continue; |
| 3086 | |
| 3087 | skb = skb_peek(&tmp->data_q); |
| 3088 | if (skb->priority < cur_prio) |
| 3089 | continue; |
| 3090 | |
| 3091 | if (skb->priority > cur_prio) { |
| 3092 | num = 0; |
| 3093 | min = ~0; |
| 3094 | cur_prio = skb->priority; |
| 3095 | } |
| 3096 | |
| 3097 | num++; |
| 3098 | |
| 3099 | if (conn->sent < min) { |
| 3100 | min = conn->sent; |
| 3101 | chan = tmp; |
| 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | if (hci_conn_num(hdev, type) == conn_num) |
| 3106 | break; |
| 3107 | } |
| 3108 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3109 | rcu_read_unlock(); |
| 3110 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3111 | if (!chan) |
| 3112 | return NULL; |
| 3113 | |
| 3114 | switch (chan->conn->type) { |
| 3115 | case ACL_LINK: |
| 3116 | cnt = hdev->acl_cnt; |
| 3117 | break; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3118 | case AMP_LINK: |
| 3119 | cnt = hdev->block_cnt; |
| 3120 | break; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3121 | case SCO_LINK: |
| 3122 | case ESCO_LINK: |
| 3123 | cnt = hdev->sco_cnt; |
| 3124 | break; |
| 3125 | case LE_LINK: |
| 3126 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 3127 | break; |
| 3128 | default: |
| 3129 | cnt = 0; |
| 3130 | BT_ERR("Unknown link type"); |
| 3131 | } |
| 3132 | |
| 3133 | q = cnt / num; |
| 3134 | *quote = q ? q : 1; |
| 3135 | BT_DBG("chan %p quote %d", chan, *quote); |
| 3136 | return chan; |
| 3137 | } |
| 3138 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3139 | static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) |
| 3140 | { |
| 3141 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 3142 | struct hci_conn *conn; |
| 3143 | int num = 0; |
| 3144 | |
| 3145 | BT_DBG("%s", hdev->name); |
| 3146 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3147 | rcu_read_lock(); |
| 3148 | |
| 3149 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3150 | struct hci_chan *chan; |
| 3151 | |
| 3152 | if (conn->type != type) |
| 3153 | continue; |
| 3154 | |
| 3155 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 3156 | continue; |
| 3157 | |
| 3158 | num++; |
| 3159 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 3160 | list_for_each_entry_rcu(chan, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3161 | struct sk_buff *skb; |
| 3162 | |
| 3163 | if (chan->sent) { |
| 3164 | chan->sent = 0; |
| 3165 | continue; |
| 3166 | } |
| 3167 | |
| 3168 | if (skb_queue_empty(&chan->data_q)) |
| 3169 | continue; |
| 3170 | |
| 3171 | skb = skb_peek(&chan->data_q); |
| 3172 | if (skb->priority >= HCI_PRIO_MAX - 1) |
| 3173 | continue; |
| 3174 | |
| 3175 | skb->priority = HCI_PRIO_MAX - 1; |
| 3176 | |
| 3177 | BT_DBG("chan %p skb %p promoted to %d", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3178 | skb->priority); |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | if (hci_conn_num(hdev, type) == num) |
| 3182 | break; |
| 3183 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3184 | |
| 3185 | rcu_read_unlock(); |
| 3186 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3187 | } |
| 3188 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3189 | static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) |
| 3190 | { |
| 3191 | /* Calculate count of blocks used by this packet */ |
| 3192 | return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); |
| 3193 | } |
| 3194 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3195 | static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 3198 | /* ACL tx timeout must be longer than maximum |
| 3199 | * link supervision timeout (40.9 seconds) */ |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3200 | if (!cnt && time_after(jiffies, hdev->acl_last_tx + |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 3201 | HCI_ACL_TX_TIMEOUT)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3202 | hci_link_tx_to(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | } |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3206 | static void hci_sched_acl_pkt(struct hci_dev *hdev) |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3207 | { |
| 3208 | unsigned int cnt = hdev->acl_cnt; |
| 3209 | struct hci_chan *chan; |
| 3210 | struct sk_buff *skb; |
| 3211 | int quote; |
| 3212 | |
| 3213 | __check_timeout(hdev, cnt); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3214 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3215 | while (hdev->acl_cnt && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3216 | (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3217 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3218 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3219 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3220 | skb->len, skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3221 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3222 | /* Stop if priority has changed */ |
| 3223 | if (skb->priority < priority) |
| 3224 | break; |
| 3225 | |
| 3226 | skb = skb_dequeue(&chan->data_q); |
| 3227 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3228 | hci_conn_enter_active_mode(chan->conn, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3229 | bt_cb(skb)->force_active); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3230 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3231 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | hdev->acl_last_tx = jiffies; |
| 3233 | |
| 3234 | hdev->acl_cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3235 | chan->sent++; |
| 3236 | chan->conn->sent++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3237 | } |
| 3238 | } |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3239 | |
| 3240 | if (cnt != hdev->acl_cnt) |
| 3241 | hci_prio_recalculate(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | } |
| 3243 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3244 | static void hci_sched_acl_blk(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3245 | { |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3246 | unsigned int cnt = hdev->block_cnt; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3247 | struct hci_chan *chan; |
| 3248 | struct sk_buff *skb; |
| 3249 | int quote; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3250 | u8 type; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3251 | |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3252 | __check_timeout(hdev, cnt); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3253 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3254 | BT_DBG("%s", hdev->name); |
| 3255 | |
| 3256 | if (hdev->dev_type == HCI_AMP) |
| 3257 | type = AMP_LINK; |
| 3258 | else |
| 3259 | type = ACL_LINK; |
| 3260 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3261 | while (hdev->block_cnt > 0 && |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3262 | (chan = hci_chan_sent(hdev, type, "e))) { |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3263 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3264 | while (quote > 0 && (skb = skb_peek(&chan->data_q))) { |
| 3265 | int blocks; |
| 3266 | |
| 3267 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3268 | skb->len, skb->priority); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3269 | |
| 3270 | /* Stop if priority has changed */ |
| 3271 | if (skb->priority < priority) |
| 3272 | break; |
| 3273 | |
| 3274 | skb = skb_dequeue(&chan->data_q); |
| 3275 | |
| 3276 | blocks = __get_blocks(hdev, skb); |
| 3277 | if (blocks > hdev->block_cnt) |
| 3278 | return; |
| 3279 | |
| 3280 | hci_conn_enter_active_mode(chan->conn, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3281 | bt_cb(skb)->force_active); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3282 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3283 | hci_send_frame(hdev, skb); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3284 | hdev->acl_last_tx = jiffies; |
| 3285 | |
| 3286 | hdev->block_cnt -= blocks; |
| 3287 | quote -= blocks; |
| 3288 | |
| 3289 | chan->sent += blocks; |
| 3290 | chan->conn->sent += blocks; |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | if (cnt != hdev->block_cnt) |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3295 | hci_prio_recalculate(hdev, type); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3296 | } |
| 3297 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3298 | static void hci_sched_acl(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3299 | { |
| 3300 | BT_DBG("%s", hdev->name); |
| 3301 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3302 | /* No ACL link over BR/EDR controller */ |
| 3303 | if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR) |
| 3304 | return; |
| 3305 | |
| 3306 | /* No AMP link over AMP controller */ |
| 3307 | if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3308 | return; |
| 3309 | |
| 3310 | switch (hdev->flow_ctl_mode) { |
| 3311 | case HCI_FLOW_CTL_MODE_PACKET_BASED: |
| 3312 | hci_sched_acl_pkt(hdev); |
| 3313 | break; |
| 3314 | |
| 3315 | case HCI_FLOW_CTL_MODE_BLOCK_BASED: |
| 3316 | hci_sched_acl_blk(hdev); |
| 3317 | break; |
| 3318 | } |
| 3319 | } |
| 3320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | /* Schedule SCO */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3322 | static void hci_sched_sco(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | { |
| 3324 | struct hci_conn *conn; |
| 3325 | struct sk_buff *skb; |
| 3326 | int quote; |
| 3327 | |
| 3328 | BT_DBG("%s", hdev->name); |
| 3329 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3330 | if (!hci_conn_num(hdev, SCO_LINK)) |
| 3331 | return; |
| 3332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { |
| 3334 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 3335 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3336 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | |
| 3338 | conn->sent++; |
| 3339 | if (conn->sent == ~0) |
| 3340 | conn->sent = 0; |
| 3341 | } |
| 3342 | } |
| 3343 | } |
| 3344 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3345 | static void hci_sched_esco(struct hci_dev *hdev) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3346 | { |
| 3347 | struct hci_conn *conn; |
| 3348 | struct sk_buff *skb; |
| 3349 | int quote; |
| 3350 | |
| 3351 | BT_DBG("%s", hdev->name); |
| 3352 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3353 | if (!hci_conn_num(hdev, ESCO_LINK)) |
| 3354 | return; |
| 3355 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 3356 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, |
| 3357 | "e))) { |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3358 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 3359 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3360 | hci_send_frame(hdev, skb); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3361 | |
| 3362 | conn->sent++; |
| 3363 | if (conn->sent == ~0) |
| 3364 | conn->sent = 0; |
| 3365 | } |
| 3366 | } |
| 3367 | } |
| 3368 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3369 | static void hci_sched_le(struct hci_dev *hdev) |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3370 | { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3371 | struct hci_chan *chan; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3372 | struct sk_buff *skb; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3373 | int quote, cnt, tmp; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3374 | |
| 3375 | BT_DBG("%s", hdev->name); |
| 3376 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3377 | if (!hci_conn_num(hdev, LE_LINK)) |
| 3378 | return; |
| 3379 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3380 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 3381 | /* LE tx timeout must be longer than maximum |
| 3382 | * link supervision timeout (40.9 seconds) */ |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3383 | if (!hdev->le_cnt && hdev->le_pkts && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3384 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3385 | hci_link_tx_to(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3389 | tmp = cnt; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3390 | while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3391 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3392 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3393 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3394 | skb->len, skb->priority); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3395 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3396 | /* Stop if priority has changed */ |
| 3397 | if (skb->priority < priority) |
| 3398 | break; |
| 3399 | |
| 3400 | skb = skb_dequeue(&chan->data_q); |
| 3401 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3402 | hci_send_frame(hdev, skb); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3403 | hdev->le_last_tx = jiffies; |
| 3404 | |
| 3405 | cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3406 | chan->sent++; |
| 3407 | chan->conn->sent++; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3408 | } |
| 3409 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3410 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3411 | if (hdev->le_pkts) |
| 3412 | hdev->le_cnt = cnt; |
| 3413 | else |
| 3414 | hdev->acl_cnt = cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3415 | |
| 3416 | if (cnt != tmp) |
| 3417 | hci_prio_recalculate(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3418 | } |
| 3419 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3420 | static void hci_tx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3421 | { |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3422 | struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | struct sk_buff *skb; |
| 3424 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3425 | BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3426 | hdev->sco_cnt, hdev->le_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | |
Marcel Holtmann | 52de599 | 2013-09-03 18:08:38 -0700 | [diff] [blame] | 3428 | if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 3429 | /* Schedule queues and send stuff to HCI driver */ |
| 3430 | hci_sched_acl(hdev); |
| 3431 | hci_sched_sco(hdev); |
| 3432 | hci_sched_esco(hdev); |
| 3433 | hci_sched_le(hdev); |
| 3434 | } |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | /* Send next queued raw (unknown type) packet */ |
| 3437 | while ((skb = skb_dequeue(&hdev->raw_q))) |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3438 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | } |
| 3440 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3441 | /* ----- HCI RX task (incoming data processing) ----- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | |
| 3443 | /* ACL data packet */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3444 | static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | { |
| 3446 | struct hci_acl_hdr *hdr = (void *) skb->data; |
| 3447 | struct hci_conn *conn; |
| 3448 | __u16 handle, flags; |
| 3449 | |
| 3450 | skb_pull(skb, HCI_ACL_HDR_SIZE); |
| 3451 | |
| 3452 | handle = __le16_to_cpu(hdr->handle); |
| 3453 | flags = hci_flags(handle); |
| 3454 | handle = hci_handle(handle); |
| 3455 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3456 | BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3457 | handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | |
| 3459 | hdev->stat.acl_rx++; |
| 3460 | |
| 3461 | hci_dev_lock(hdev); |
| 3462 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 3463 | hci_dev_unlock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | if (conn) { |
Mat Martineau | 65983fc | 2011-12-13 15:06:02 -0800 | [diff] [blame] | 3466 | hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 3469 | l2cap_recv_acldata(conn, skb, flags); |
| 3470 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3472 | BT_ERR("%s ACL packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3473 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | kfree_skb(skb); |
| 3477 | } |
| 3478 | |
| 3479 | /* SCO data packet */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3480 | static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | { |
| 3482 | struct hci_sco_hdr *hdr = (void *) skb->data; |
| 3483 | struct hci_conn *conn; |
| 3484 | __u16 handle; |
| 3485 | |
| 3486 | skb_pull(skb, HCI_SCO_HDR_SIZE); |
| 3487 | |
| 3488 | handle = __le16_to_cpu(hdr->handle); |
| 3489 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3490 | BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | |
| 3492 | hdev->stat.sco_rx++; |
| 3493 | |
| 3494 | hci_dev_lock(hdev); |
| 3495 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 3496 | hci_dev_unlock(hdev); |
| 3497 | |
| 3498 | if (conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 3500 | sco_recv_scodata(conn, skb); |
| 3501 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3503 | BT_ERR("%s SCO packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3504 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | kfree_skb(skb); |
| 3508 | } |
| 3509 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3510 | static bool hci_req_is_complete(struct hci_dev *hdev) |
| 3511 | { |
| 3512 | struct sk_buff *skb; |
| 3513 | |
| 3514 | skb = skb_peek(&hdev->cmd_q); |
| 3515 | if (!skb) |
| 3516 | return true; |
| 3517 | |
| 3518 | return bt_cb(skb)->req.start; |
| 3519 | } |
| 3520 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3521 | static void hci_resend_last(struct hci_dev *hdev) |
| 3522 | { |
| 3523 | struct hci_command_hdr *sent; |
| 3524 | struct sk_buff *skb; |
| 3525 | u16 opcode; |
| 3526 | |
| 3527 | if (!hdev->sent_cmd) |
| 3528 | return; |
| 3529 | |
| 3530 | sent = (void *) hdev->sent_cmd->data; |
| 3531 | opcode = __le16_to_cpu(sent->opcode); |
| 3532 | if (opcode == HCI_OP_RESET) |
| 3533 | return; |
| 3534 | |
| 3535 | skb = skb_clone(hdev->sent_cmd, GFP_KERNEL); |
| 3536 | if (!skb) |
| 3537 | return; |
| 3538 | |
| 3539 | skb_queue_head(&hdev->cmd_q, skb); |
| 3540 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 3541 | } |
| 3542 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3543 | void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status) |
| 3544 | { |
| 3545 | hci_req_complete_t req_complete = NULL; |
| 3546 | struct sk_buff *skb; |
| 3547 | unsigned long flags; |
| 3548 | |
| 3549 | BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); |
| 3550 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3551 | /* If the completed command doesn't match the last one that was |
| 3552 | * sent we need to do special handling of it. |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3553 | */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3554 | if (!hci_sent_cmd_data(hdev, opcode)) { |
| 3555 | /* Some CSR based controllers generate a spontaneous |
| 3556 | * reset complete event during init and any pending |
| 3557 | * command will never be completed. In such a case we |
| 3558 | * need to resend whatever was the last sent |
| 3559 | * command. |
| 3560 | */ |
| 3561 | if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET) |
| 3562 | hci_resend_last(hdev); |
| 3563 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3564 | return; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3565 | } |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3566 | |
| 3567 | /* If the command succeeded and there's still more commands in |
| 3568 | * this request the request is not yet complete. |
| 3569 | */ |
| 3570 | if (!status && !hci_req_is_complete(hdev)) |
| 3571 | return; |
| 3572 | |
| 3573 | /* If this was the last command in a request the complete |
| 3574 | * callback would be found in hdev->sent_cmd instead of the |
| 3575 | * command queue (hdev->cmd_q). |
| 3576 | */ |
| 3577 | if (hdev->sent_cmd) { |
| 3578 | req_complete = bt_cb(hdev->sent_cmd)->req.complete; |
Johan Hedberg | 53e21fb | 2013-07-27 14:11:14 -0500 | [diff] [blame] | 3579 | |
| 3580 | if (req_complete) { |
| 3581 | /* We must set the complete callback to NULL to |
| 3582 | * avoid calling the callback more than once if |
| 3583 | * this function gets called again. |
| 3584 | */ |
| 3585 | bt_cb(hdev->sent_cmd)->req.complete = NULL; |
| 3586 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3587 | goto call_complete; |
Johan Hedberg | 53e21fb | 2013-07-27 14:11:14 -0500 | [diff] [blame] | 3588 | } |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | /* Remove all pending commands belonging to this request */ |
| 3592 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 3593 | while ((skb = __skb_dequeue(&hdev->cmd_q))) { |
| 3594 | if (bt_cb(skb)->req.start) { |
| 3595 | __skb_queue_head(&hdev->cmd_q, skb); |
| 3596 | break; |
| 3597 | } |
| 3598 | |
| 3599 | req_complete = bt_cb(skb)->req.complete; |
| 3600 | kfree_skb(skb); |
| 3601 | } |
| 3602 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 3603 | |
| 3604 | call_complete: |
| 3605 | if (req_complete) |
| 3606 | req_complete(hdev, status); |
| 3607 | } |
| 3608 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3609 | static void hci_rx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | { |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3611 | struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | struct sk_buff *skb; |
| 3613 | |
| 3614 | BT_DBG("%s", hdev->name); |
| 3615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | while ((skb = skb_dequeue(&hdev->rx_q))) { |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 3617 | /* Send copy to monitor */ |
| 3618 | hci_send_to_monitor(hdev, skb); |
| 3619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | if (atomic_read(&hdev->promisc)) { |
| 3621 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 3622 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 3625 | if (test_bit(HCI_RAW, &hdev->flags) || |
| 3626 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | kfree_skb(skb); |
| 3628 | continue; |
| 3629 | } |
| 3630 | |
| 3631 | if (test_bit(HCI_INIT, &hdev->flags)) { |
| 3632 | /* Don't process data packets in this states. */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3633 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | case HCI_ACLDATA_PKT: |
| 3635 | case HCI_SCODATA_PKT: |
| 3636 | kfree_skb(skb); |
| 3637 | continue; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | /* Process frame */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3642 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | case HCI_EVENT_PKT: |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3644 | BT_DBG("%s Event packet", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | hci_event_packet(hdev, skb); |
| 3646 | break; |
| 3647 | |
| 3648 | case HCI_ACLDATA_PKT: |
| 3649 | BT_DBG("%s ACL data packet", hdev->name); |
| 3650 | hci_acldata_packet(hdev, skb); |
| 3651 | break; |
| 3652 | |
| 3653 | case HCI_SCODATA_PKT: |
| 3654 | BT_DBG("%s SCO data packet", hdev->name); |
| 3655 | hci_scodata_packet(hdev, skb); |
| 3656 | break; |
| 3657 | |
| 3658 | default: |
| 3659 | kfree_skb(skb); |
| 3660 | break; |
| 3661 | } |
| 3662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | } |
| 3664 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3665 | static void hci_cmd_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | { |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3667 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | struct sk_buff *skb; |
| 3669 | |
Andrei Emeltchenko | 2104786 | 2012-07-10 15:27:47 +0300 | [diff] [blame] | 3670 | BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, |
| 3671 | atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | /* Send queued commands */ |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 3674 | if (atomic_read(&hdev->cmd_cnt)) { |
| 3675 | skb = skb_dequeue(&hdev->cmd_q); |
| 3676 | if (!skb) |
| 3677 | return; |
| 3678 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 3679 | kfree_skb(hdev->sent_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | |
Marcel Holtmann | a675d7f | 2013-09-03 18:11:07 -0700 | [diff] [blame] | 3681 | hdev->sent_cmd = skb_clone(skb, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 3682 | if (hdev->sent_cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | atomic_dec(&hdev->cmd_cnt); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3684 | hci_send_frame(hdev, skb); |
Szymon Janc | 7bdb8a5 | 2011-07-26 22:46:54 +0200 | [diff] [blame] | 3685 | if (test_bit(HCI_RESET, &hdev->flags)) |
| 3686 | del_timer(&hdev->cmd_timer); |
| 3687 | else |
| 3688 | mod_timer(&hdev->cmd_timer, |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 3689 | jiffies + HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | } else { |
| 3691 | skb_queue_head(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3692 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | } |
| 3694 | } |
| 3695 | } |